开发者

Java: Why isn't my file writing working?

开发者 https://www.devze.com 2022-12-12 04:48 出处:网络
I\'m trying to write a file from my Java program, but nothing happens. I\'m not getting any exceptions or errors, it\'s just silently failing.

I'm trying to write a file from my Java program, but nothing happens. I'm not getting any exceptions or errors, it's just silently failing.

        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;
        }

Here is the TreePrinter function:

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());
}

What am I doing wrong?


Close and / or flush your output stream:

TreePrinter.printNewickFormat(tree, output);
output.close(); // <-- this is the missing part
} catch (IOException e) {

Additionally, calling delete() / createNewFile() is unnecessary - your output stream will either create or overwrite an existing file.


flush the PrintStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号