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

Java 即使在关闭InputStream后也无法删除文件,java,inputstream,Java,Inputstream,读取文件后,我试图将其删除,但出现以下错误: java.io.IOException:无法删除位于的文件test.zip org.apache.commons.io.FileUtils.forceDelete FileUtils.java:1390 org.apache.commons.io.FileUtils.cleanDirectory FileUtils.java:1044 位于org.apache.commons.io.FileUtils.deleteDirectory java:977

读取文件后,我试图将其删除,但出现以下错误:

java.io.IOException:无法删除位于的文件test.zip org.apache.commons.io.FileUtils.forceDelete FileUtils.java:1390 org.apache.commons.io.FileUtils.cleanDirectory FileUtils.java:1044 位于org.apache.commons.io.FileUtils.deleteDirectory java:977

这是我的密码。我一直很小心地在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();
}

经过一些测试,我发现我可以在行is=newurl filePath.openStream;之前手动删除该文件;。在它之后,甚至在线路关闭之后;除非停止程序,否则无法手动删除该文件。

我得到了它。我是通过url is=new URLfilePath.openStream;,打开文件的;,并尝试按绝对路径删除它。我将其更改为is=新文件输入流绝对文件路径;而且它有效

 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();
}