【发布时间】:2015-06-18 21:54:54
【问题描述】:
我正在寻找一种通过搜索特定字符串并将其写入文本文件的方法。我的意思是我想将一个文本文件分成 2 个部分,以便我可以使用单独的 java 程序写入每个部分。我这样做的原因是我正在使用 2 个不同的 java 程序运行测试并将结果存储在 2 个文本文件中,但我希望它们都写入同一个地方,在不同的部分。这是我目前用来编写的代码,但我知道这可能都需要更改才能完成我想做的事情。
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String content = dateFormat.format(cal.getTime()) + " Test took: " + Test3 + " seconds.";
File file = new File("mypath\\Test.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter("mypath\\Test.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
fw.write("\r\n");
bw.write(content);
fw.write("\r\n");
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
它当前存储每个测试的结果,以 1 行分隔。我希望做的是将 2 个部分放入同一个文本文件中,并且能够独立添加到每个部分的末尾。这甚至可能吗?如果是这样,我将如何去做?
【问题讨论】:
-
听起来像是 xy 问题。实际写入磁盘的内容是什么?