Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java 8中使用Files.newDirectoryStream()时如何关闭文件处理程序_Java_File_Unmount - Fatal编程技术网

在java 8中使用Files.newDirectoryStream()时如何关闭文件处理程序

在java 8中使用Files.newDirectoryStream()时如何关闭文件处理程序,java,file,unmount,Java,File,Unmount,我们有一个linux应用程序,由于下面的调用,我们无法卸载USB驱动器。是否需要一个文件资源来正确关闭流 Files.newDirectoryStream( Paths.get(importDir), path -> path.toString().endsWith(".ini") && path.toFile().isFile()) .forEach(path -> importItems.ad

我们有一个linux应用程序,由于下面的调用,我们无法卸载USB驱动器。是否需要一个文件资源来正确关闭流

     Files.newDirectoryStream(
         Paths.get(importDir), 
         path -> path.toString().endsWith(".ini") && path.toFile().isFile())
            .forEach(path -> importItems.add(path));
以下是响应的输出:

umount: /media/flashdrive: target is busy
    (In some cases useful info about processes that
     use the device is found by lsof(8) or fuser(1).)

我们目前正在使用java 8。

您需要关闭打开的目录流:

未能关闭流可能导致资源泄漏。try with resources语句提供了一个有用的构造来确保流是关闭的

try(DirectoryStream路径=Files.newDirectoryStream(
获取路径(importDir),
path->path.toString().endsWith(“.ini”)&&path.toFile().isFile()){
path.forEach(path->importItems.add(path));
}
另一种方法是直接调用
path.close()

    try (DirectoryStream<Path> paths = Files.newDirectoryStream(
            Paths.get(importDir),
            path -> path.toString().endsWith(".ini") && path.toFile().isFile())) {

        paths.forEach(path -> importItems.add(path));
    }