tags:

views:

97

answers:

2

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?

+3  A: 

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.

ChssPly76
In that case your node must be empty - nothing is written to the file. You can test whether that's the case (or you have some other issue) by printing something prior to close, e.g. `output.println("test");`
ChssPly76
A: 

flush the PrintStream.

Summy