如何 read/write Java 中的文件

How to read/write a file in Java

我想根据一些正则表达式替换文件中的一些项目。为此:

完成所有这些后,我尝试删除该文件(以便用替换的行重新创建它)。

出于某种原因,这不起作用:似乎 Java 保留该文件的句柄,即使在 BufferedReader 已关闭后也是如此。

有人能解决这个(新手)问题吗?

代码摘录:

      Pattern oDatePattern   = Pattern.compile("at \d{2}:\d{2}:\d{2} "); // meaning: "at xx:xx:xx"
      Pattern oTimePattern   = Pattern.compile("Kernel time [0-9]*\.?[0-9]+ User time: [0-9]*\.?[0-9]+"); // "[0-9]*\.?[0-9]+" stands for any floating point number
      Pattern oMemoryPattern = Pattern.compile("\([0-9,A-F]*\)"); // "[0-9,A-F]*" stands for any hexadecimal number 
      Matcher oDateMatcher;
      Matcher oTimeMatcher;
      Matcher oMemoryMatcher;

      List<String> sLog_Content = new ArrayList<String>();

      BufferedReader br = new BufferedReader(new FileReader(sLp_LogFile));
      try {
        String sLine = br.readLine();

        while (sLine != null) {
          System.out.println("ORIG : " + sLine);
          oDateMatcher = oDatePattern.matcher(sLine);
          sLine        = oDateMatcher.replaceAll("at <timestamp> ");
          oTimeMatcher = oTimePattern.matcher(sLine);
          sLine        = oTimeMatcher.replaceAll("Kernel time <Kernel_Time_usage> User time: <User_Time_usage>");
          oMemoryMatcher = oMemoryPattern.matcher(sLine);
          sLine          = oMemoryMatcher.replaceAll("<Memory_Address>");
          System.out.println("REPL : " + sLine);
          sLog_Content.add(sLine);
          sLine = br.readLine();
        }
      } finally {
        br.close();
      }

      System.out.println("All lines are read and regex replaced, try to delete the file");

      File tst_File = new File(sLp_LogFile);
      if (tst_File.exists()) {
        System.out.println(sLp_LogFile + " exists");
      } else {
        System.out.println(sLp_LogFile + " does not exist");
      }

      if (tst_File.delete()) {
        System.out.println(sLp_LogFile + " is deleted");
      } else {
        System.out.println(sLp_LogFile + " is not deleted");
      }

输出日志:

ORIG : Reading buffer 1 (0000000002ED0070) at 15:40:44 (index 125999, size 4410000 lines 126000, total lines read 126000)
REPL : Reading buffer 1 <Memory_Address> at <timestamp> (index 125999, size 4410000 lines 126000, total lines read 126000)
...
ORIG : Sending buffer 1 (0000000002ED0070) at 15:40:44 (index 125999, size 4410000, lines 126000, total lines sent 126000)
REPL : Sending buffer 1 <Memory_Address> at <timestamp> (index 125999, size 4410000, lines 126000, total lines sent 126000)
...
ORIG : Kernel time 0.2808 User time: 0.312
REPL : Kernel time <Kernel_Time_usage> User time: <User_Time_usage>
...
All lines are read and regex replaced, try to delete the file
D:\Logfile_lp.log exists
D:\Logfile_lp.log is not deleted

一个可能的解释是您的应用程序在其他地方打开了文件。

或者它可能是打开了该文件的另一个应用程序。

或者应用程序/用户可能有读取文件的权限但没有删除它的权限。


我同意使用 Files.delete ..

的建议

我发现你的代码没有问题。

看似关闭 BufferReader 确保文件已关闭。 (比照this response)。

也许你可以试试Files.delete cf this response
它将通过抛出不同的异常来提供有关删除失败的更多信息。

我是初学者,我知道的东西没有你多。但如果我是对的,你应该先将你的更改保存在一个临时文件中。之后您将再次读取临时文件,稍后您将写入您的真实文件。希望我的评论对您有所帮助。

下午好, 我要感谢大家寻找这个问题的解决方案。不幸的是,问题不是基于 Java:我试图写入的文件是由重定向 cmd /c <program>.exe >> <output>.log 创建的,而且似乎 Windows 还没有将输出缓冲区完全刷新到输出文件,创建问题。

我目前正在使用以下(非常肮脏的)解决方法来解决此问题:

boolean bFile_can_be_opened = false;
while (!bFile_can_be_opened) {
  try {
    fwLog2 = new FileWriter(sLp_LogFile, true);
    bFile_can_be_opened = true;
  }
  catch (Exception e)
  {}
}

有关此问题的更多信息可以在以下新的 Whosebug 问题下找到:How to release a file, locked by the application, in Java