简体   繁体   English

即使关闭 InputStream 也无法删除文件

[英]Cannot delete file even after closing InputStream

After reading a file, I'm trying to delete it, but the following error appears:读取文件后,我试图删除它,但出现以下错误:

java.io.IOException: Unable to delete file test.zip at org.apache.commons.io.FileUtils.forceDelete (FileUtils.java:1390) at org.apache.commons.io.FileUtils.cleanDirectory (FileUtils.java:1044) at org.apache.commons.io.FileUtils.deleteDirectory (FileUtils.java:977) java.io.IOException: 无法删除 org.apache.commons.io.FileUtils.forceDelete (FileUtils.java:1390) at org.apache.commons.io.FileUtils.cleanDirectory (FileUtils.java:1044) 中的文件 test.zip ) 在 org.apache.commons.io.FileUtils.deleteDirectory (FileUtils.java:977)

Here is my code.这是我的代码。 I've been careful to close the InputStream in the finally clause and only then call the method that deletes the file, but even then I can only delete it when I stop the program.我一直很小心地在 finally 子句中关闭 InputStream ,然后才调用删除文件的方法,但即使如此,我也只能在停止程序时删除它。

 InputStream is = null;
 try {
     is = new URL(filePath).openStream(); // filePath is a string containing the path to the file like http://test.com/file.zip
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
     String line;
     StringBuilder sb = new StringBuilder();

     while ((line = reader.readLine()) != null) {
         sb.append(line.trim());
     }

     String xml = sb.toString(); // this code is working, the result of the "xml" variable is as expected
  } catch (Exception e) {
      e.printStackTrace();
  } finally {
      try {
          if (is != null) {
              is.close();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }

      removeFileAndFolder(absolutePath);
  }


private void removeFileAndFolder(String absolutePath) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String folder = getFolder(absolutePath); // this method just get the path to the folder, because I want to delete the entire folder, but the error occurs when deleting the file
                FileUtils.deleteDirectory(new File(folder));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

After some tests I discovered that I can manually delete the file just before the line "is = new URL (filePath) .openStream ();".经过一些测试,我发现我可以在“is = new URL (filePath) .openStream ();”行之前手动删除文件。 After it, and even after the line "is.close ();"在它之后,甚至在“is.close();”行之后I can not delete the file manually unless I stop the program.除非我停止程序,否则我无法手动删除文件。

Maybe I have missread your question but you wrote: 也许我错过了您的问题,但您写道:

// filePath is a string containing the path to the file like http://test.com/file.zip // filePath是一个字符串,其中包含文件的路径,例如http://test.com/file.zip

Therefore you want to delete a file (directory) at a webserver and not on your local computer or your LAN? 因此,您要删除Web服务器上的文件(目录),而不是本地计算机或LAN上的文件吗? In this case you need to issue some FTP-commands (eg Apache FTP-client ) 在这种情况下,您需要发出一些FTP命令(例如Apache FTP-client

The best idea is to start with Apache Commons.net and to use only the parts you need (eg FTP/FTPS). 最好的主意是从Apache Commons.net开始,仅使用所需的部分(例如FTP / FTPS)。

import org.apache.commons.net.ftp.FTPClient;
[..]
FTPClient ftpClient = new FTPClient();    
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.deleteFile(fileToDelete);
ftpClient.logout();
ftpClient.disconnect();

I got it. 我知道了。 I was opening the file by the url (is = new URL(filePath).openStream();), and trying to delete it by absolute path. 我正在通过url(is = new URL(filePath).openStream();)打开文件,并尝试通过绝对路径将其删除。 I changed it to "is = new FileInputStream(absoluteFilePath);" 我将其更改为“ is = new FileInputStream(absoluteFilePath);” and it works! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM