Java:为什么不是';我的文件写不好吗?
我试图从我的Java程序中编写一个文件,但什么也没发生。我没有收到任何异常或错误,只是默默地失败了Java:为什么不是';我的文件写不好吗?,java,file,io,Java,File,Io,我试图从我的Java程序中编写一个文件,但什么也没发生。我没有收到任何异常或错误,只是默默地失败了 try { File outputFile = new File(args[args.length - 1]); outputFile.delete(); outputFile.createNewFile(); PrintStream output = new PrintStream(n
try {
File outputFile = new File(args[args.length - 1]);
outputFile.delete();
outputFile.createNewFile();
PrintStream output = new PrintStream(new FileOutputStream(outputFile));
TreePrinter.printNewickFormat(tree, output);
} catch (IOException e) {
e.printStackTrace();
return;
}
以下是TreePrinter
功能:
public static void printNewickFormat(PhylogenyTree node, PrintStream stream) {
if (node.getChildren().size() > 0) {
stream.print("(");
int i = 1;
for (PhylogenyTree pt : node.getChildren()) {
printNewickFormat(pt, stream);
if (i != node.getChildren().size()) {
stream.print(",");
}
i++;
}
stream.print(")");
}
stream.format("[%s]%s", node.getAnimal().getLatinName(), node.getAnimal().getName());
}
我做错了什么?关闭和/或刷新输出流:
TreePrinter.printNewickFormat(tree, output);
output.close(); // <-- this is the missing part
} catch (IOException e) {
TreePrinter.printNewickFormat(树,输出);
output.close();// 关闭和/或刷新输出流:
TreePrinter.printNewickFormat(tree, output);
output.close(); // <-- this is the missing part
} catch (IOException e) {
TreePrinter.printNewickFormat(树,输出);
output.close();// 刷新打印流。刷新打印流。在这种情况下,节点必须为空-不会向文件写入任何内容。您可以通过在关闭之前打印一些内容来测试是否是这种情况(或者您有其他问题),例如output.println(“测试”)代码>在这种情况下,节点必须为空-不会向文件写入任何内容。您可以通过在关闭之前打印一些内容来测试是否是这种情况(或者您有其他问题),例如output.println(“测试”)代码>该代码保证至少创建一个(可能为空)文件或引发一些异常。该代码保证至少创建一个(可能为空)文件或引发一些异常。