gpt4 book ai didi

Java:为什么我的文件写入不起作用?

转载 作者:行者123 更新时间:2023-12-01 17:40:12 25 4
gpt4 key购买 nike

我正在尝试从 Java 程序写入文件,但没有任何反应。我没有收到任何异常或错误,它只是默默地失败。

        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) {

此外,调用 delete()/createNewFile() 是不必要的 - 您的输出流将创建或覆盖现有文件。

关于Java:为什么我的文件写入不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1768083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com