相关疑难解决方法(0)

当流未明确关闭时,Files.list(Path dir)中的资源泄漏?

我最近写了一个小应用程序,定期检查目录的内容.过了一会儿,应用程序因为打开文件句柄太多而崩溃.经过一些调试,我发现以下行中的错误:

Files.list(Paths.get(destination)).forEach(path -> {
     // To stuff
});
Run Code Online (Sandbox Code Playgroud)

然后我检查了javadoc(我可能应该早点完成)Files.list并找到:

* <p> The returned stream encapsulates a {@link DirectoryStream}.
* If timely disposal of file system resources is required, the
* {@code try}-with-resources construct should be used to ensure that the
* stream's {@link Stream#close close} method is invoked after the stream
* operations are completed
Run Code Online (Sandbox Code Playgroud)

对我来说,"及时处理"仍然听起来资源将最终在应用程序退出之前发布.我查看了JDK(1.8.60)代码,但是我无法找到任何关于通过Files.list再次发布打开的文件句柄的提示.

然后我创建了一个小应用程序,在使用后显式调用垃圾收集器Files.list:

while (true) {
    Files.list(Paths.get("/")).forEach(path -> {
      System.out.println(path);
    });
    Thread.sleep(5000);

    System.gc();
    System.runFinalization();
}
Run Code Online (Sandbox Code Playgroud)

当我检查打开的文件句柄时,lsof -p <pid>我仍然可以看到"/"的打开文件句柄列表变得越来越长. …

java stream java-8

22
推荐指数
1
解决办法
5606
查看次数

终端操作是否关闭了流?

dirPath包含200k文件.我想逐个阅读它们并做一些处理.以下代码片段会导致java.nio.file.FileSystemException: dirPath/file-N Too many open files.终端操作是否forEach()应该在移动到下一个之前关闭开放流(即打开文件)?换句话说,我是否必须为流式文件添加try-with-resources?

Files.list(dirPath)
     .forEach(filePath -> {
              Files.lines(filePath).forEach() { ... }
              });
Run Code Online (Sandbox Code Playgroud)

java-8 java-stream

20
推荐指数
2
解决办法
3799
查看次数

Java,IO - 删除文件的最快方法

我的问题是我有一个应用程序正在编写大量相对(100-500kb)的小型CSV文件(数十万和数十万).然后通过sql loader调用(它的oracle db)将这些文件的内容加载到数据库中,这就是我必须要使用的内容.

因此,我需要不时删除这些小文件,以防止它们占用所有空间.我想将其附加到写入这些文件的活动,并将它们作为最后的最终步骤加载到db中.

我的问题是 - 如何在java中删除一堆性能开销较小的小文件?

提前致谢!迈克尔

java io nio file

17
推荐指数
1
解决办法
2万
查看次数

删除Java中的所有空文件夹

我想编写一个删除所有空文件夹的函数,可以选择忽略某些文件类型(允许的文件类型存储在哈希图中)并判断它是否应该在目录中查找。

调用:

HashMap<String, Boolean> allowedFileTypes = new HashMap<String, Boolean>();
allowedFileTypes.put("pdf", true);
deleteEmptyFolders("ABSOLUTE PATH", allowedFileTypes, true);
Run Code Online (Sandbox Code Playgroud)

功能:

public static void deleteEmptyFolders(String folderPath, HashMap<String, Boolean> allowedFileTypes, boolean followDirectory) {

    File targetFolder = new File(folderPath);
    File[] allFiles = targetFolder.listFiles();


    if (allFiles.length == 0)
        targetFolder.delete();

    else {
        boolean importantFiles = false;

        for (File file : allFiles) {

            String fileType = "folder";
            if (!file.isDirectory())
                fileType = file.getName().substring(file.getName().lastIndexOf('.') + 1);

            if (!importantFiles)
                importantFiles = (allowedFileTypes.get(fileType) != null);

            if (file.isDirectory() && followDirectory)
                deleteEmptyFolders(file.getAbsolutePath(), allowedFileTypes, followDirectory);
        }

        // …
Run Code Online (Sandbox Code Playgroud)

java directory recursion file

5
推荐指数
1
解决办法
1万
查看次数

如何绕过java.nio.file.DirectoryNotEmptyException?

有没有办法绕过 a java.nio.file.DirectoryNotEmptyException?我希望能够删除其中包含内容的文件夹。

java nio file

5
推荐指数
1
解决办法
3万
查看次数

Selenium WebDriver - 测试过程中删除cookies并重启浏览器

我有一个测试用例来测试删除cookies并重新启动浏览器后购物车项目是否仍然存在

  1. 将商品添加到购物车(将计算添加的商品数量)
  2. Delete the cookies 在浏览器上,
  3. Close the browser
  4. Relaunch the browser
  5. 检查购物车计数项目是否仍然存在

我的主要代码如下。

// 1. Add items to shopping cart
// code go here

// 2. Clear the cookies on the browser
driver.manage().deleteAllCookies();         
driver.navigate().refresh();
// 3. Close the browser
driver.close();                             

ProfilesIni firProfiles = new ProfilesIni();        
FirefoxProfile wbdrverprofile = firProfiles.getProfile("default");  
// 4. Relaunch the browser      
driver = new FirefoxDriver(wbdrverprofile);                 
driver.get("http://www.google.com");        

Boolean isCartCountExists = driver.findElements(By.id(cartCount)).size()!=0;
if(isCartCountExists == true){
    System.out.println("Test Failed.");
}else{
    System.out.println("Test Passed.")
}
Run Code Online (Sandbox Code Playgroud)

@AfterTest 代码

@AfterTest
    public void closeBrowser(){

        driver.close(); …
Run Code Online (Sandbox Code Playgroud)

java cookies testng selenium selenium-webdriver

5
推荐指数
1
解决办法
1万
查看次数