繁体   English   中英

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

[英]Java: Why isn't my file writing working?

我正在尝试从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() -输出流将创建或覆盖现有文件。

刷新PrintStream。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM