我刚刚写了很多代码,我正在尝试用这段代码写一个输出文件-
File outfile = new File ("output.txt");
PrintWriter outFile = new PrintWriter(outfile);
outFile.printf("%-20s%5s%5s%5s%5s", f,median,avg,min,max);
outFile.println("");
outFile.printf("%-20s%5s%5s%5s%5s", f,median,avg,min,max);
outFile.println("");
outFile.printf("%-27s%5d%5d%5d%5d", firstplayer,P1median,P1avg,PScore.get(0),PScore.get(PScore.size()-1));
outFile.println("");
outFile.printf("%-27s%5d%5d%5d%5d", secondplayer,P2median,P2avg,Player2.get(0),Player2.get(Player2.size()-1));
outFile.println("");
outFile.printf("%-27s%5d%5d%5d%5d",fourthplayer,P4median,P4avg,Player4.get(0),Player4.get(Player4.size()-1));
outFile.close();
现在我没有抛出任何错误或异常,我相信我已经成功地编写了一个输出文件,但是我不知道它位于何处以及如何检查它的内容。如果这是一个糟糕的问题,我深表歉意,但我一直在努力寻找其他地方的答案,因此非常感谢任何帮助找到输出文件或纠正我的错误。
最佳答案
如果您仍然不确定它的位置,这将打印出完整的文件路径:
System.out.println(outfile.getAbsolutePath());
如果你想打印出文件的内容以便在你的程序中检查它:
//ensure content in file is updated
outfile.flush();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(outfile));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
确保同时关闭该文件。
关于Java 文件输出检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000957/