0

I have been trying to get the output of this code to be printed on a .txt file, however I getting an empty file as an output, but I see the correct results in the console, so the problem is with the StreamWriter I think.

So what am I doing wrong?

package Hierarchies;

    public static void generateHierarchy() {

        Set<Entry<Long, Node>> iterator = graph.entrySet();
        int i =0;
        for(Entry<Long,Node> e : iterator) {
            System.out.println(i++ +" - " +e.getValue().firstParents());
        }

    }

    @SuppressWarnings({ "resource" })
    public static void main(String[] args) throws JWNLException {
        // TODO Auto-generated method stub
        dictionary = Dictionary.getDefaultResourceInstance();

        File dir = new File("C:/Users/D060891/Desktop/Thesis/sentencesNYT");

        for (File file : dir.listFiles()) {

            try {
                BufferedReader input = new BufferedReader(new FileReader(file));
                String line;

                while ((line = input.readLine()) != null) {
                    demonstrateListHelper(line);                              
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        generateHierarchy();
        PrintStream out;
        try {
            out = new PrintStream(new FileOutputStream("output.txt"));
            System.setOut(out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
LuckyOwl
  • 15
  • 4
  • where's your file writer? – Haifeng Zhang Apr 24 '16 at 15:26
  • 1
    Possible duplicate of [How to create a file and write to a file in Java?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) – pczeus Apr 24 '16 at 15:27
  • 1
    Possible reasons: 1) You don't flush or close the PrintSteam. 2) Your directory is empty and no file are read. 3) All the files in that directory are empty. – OneCricketeer Apr 24 '16 at 15:29
  • 1
    Can you please add the `demonstrateListHelper` method? Another reason: You don't `System.setOut` until after you looped over all the files. – OneCricketeer Apr 24 '16 at 15:32
  • 1
    I do not see you writting anything to the file. I just see how you create a PrintStream to a file and assign it to System.out, but then you do not print nothing. Call `generateHierarchy()` after you asign out – Nadir Apr 24 '16 at 15:32

3 Answers3

0

Flush and close the stream. It might be the case that program is ending before data is flushed into the disk. Note that higher level streams maintain a buffer and hence don't write directly to disk.

hellboy
  • 85
  • 2
  • 8
0

I do not see you writting anything to the file. I just see how you create a PrintStream to a file and assign it to System.out, but then you do not print nothing. Call generateHierarchy() after you asign out

Nadir
  • 1,799
  • 12
  • 20
  • well that was a bit embarrassing, I see what's happening now ... I've only started to code 6 months ago and for some reason I keep doing such silly mistakes, thank you :) – LuckyOwl Apr 24 '16 at 15:44
  • @LuckyOwl We all have been there. Thats why stackoverflow exists ;) – Nadir Apr 24 '16 at 15:45
-1

You try to write the file by redirecting System.out, but redirect it only after the data has been written to System.out.

You should be doing something like:

void printHierarchy(PrintStream ps) {
    ...
    ps.println(...)
}

and in main():

try (PrintStream ps = new PrintStream(new FileOutputStream("output.txt"))) {
    printHierarchy(ps);
}
meriton
  • 68,356
  • 14
  • 108
  • 175