我想将列表的内容写入文件。代码很容易理解,有时也能完成工作。但大多数时候它都会失败。这是我到目前为止所尝试过的:
public void finishTheTest(String outputFileName) throws FileNotFoundException{
StringBuilder testOutPutStringBuilder = new StringBuilder();
if(!isFieldSuccessFull) {
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("Error for Task -> " + tm.getFieldHM(tm.getKey()));
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("*****************************************");
testOutPutStringBuilder.append(System.getProperty("line.separator"));
for(String str : listFailure) {
testOutPutStringBuilder.append(str);
testOutPutStringBuilder.append(System.getProperty("line.separator"));
}
testOutPutStringBuilder.append("*****************************************");
testOutPutStringBuilder.append(System.getProperty("line.separator"));
}
if(!isCinematicSuccessFull) {
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("Error for Repository -> " + tm.getFieldHM(tm.getKey()));
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("*****************************************");
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("Cinematic error.");
testOutPutStringBuilder.append(System.getProperty("line.separator"));
testOutPutStringBuilder.append("*****************************************");
testOutPutStringBuilder.append(System.getProperty("line.separator"));
}
try(PrintStream ps = new PrintStream( new FileOutputStream(outputFileName, true))) {
System.out.println(testOutPutStringBuilder);
ps.println(testOutPutStringBuilder.toString());
}
我正在浏览字符串列表来构建 StringBuilder,或者我只是将 String 附加到 StringBuilder。然后我将其打印在标准输出上,该输出正在工作,然后将其打印在指定的文件中。该文件已创建,但大多数时候即使在执行代码后它仍为空。由于我在创建 FileOutputStream 时指定了“true”,因此我希望文本仅添加在文件末尾。但是当我多次运行代码时,文件中就没有任何内容了。
我有什么遗漏的吗?
您正在使用 try-with-resources,这使得存在 抑制异常 :
的可能性https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html
如果您在此处查看已接受的答案
我必须关闭被PrintStream包裹的FileOutputStream吗?
您可以添加一个
catch
块来查看是否发生了您遗漏的某些异常。
您可以暂时将代码更改为如下所示:
public void finishTheTest(String outputFileName) throws Exception {
...
try(PrintStream ps = new PrintStream( new FileOutputStream(outputFileName, true))) {
System.out.println(testOutPutStringBuilder);
ps.println(testOutPutStringBuilder.toString());
}
catch (Exception e) {
System.out.println("Error in writing to file:");
e.printStackTrace();
throw e;
}
...
}
那么如果您的
try()
抛出异常,您将看到任何异常。
我找到了解决问题的方法。而不是使用它来写入我的文件:
try(PrintStream ps = new PrintStream( new FileOutputStream(outputFileName, true))) {
ps.println(testOutPutStringBuilder.toString());
}
我正在用这个:
try(PrintWriter ps = new PrintWriter( new BufferedWriter(new FileWriter(outputFileName, true)))) {
ps.println(testOutPutStringBuilder.toString());
}
从现在开始,这每次都有效。我仍然不知道其他代码有什么问题。