2

I am new to java programming and have been trying for the longest time to get the following code to work but although the program runs fine I don't seem to be seeing any output in the text file I am redirecting the output to. If someone could please help me out I would be very obliged. The following is the entire code. The problem I am having is that only a single line is being written to text file and I only have one line in the file at any point in time. I know this has something to do with the loop and the object being reinitialized every time but I don't know how to overcome this problem which I know is going to turn out to be a very basic mistake. But I just can't see it at the moment, so any help would be greatly appreciated. And thanks to all the people who have already tried to help, your suggestions were very helpful.

 PrintStream out;
    for(int count = 0; count<list2.size(); count++)
    {

            String originalString = list2.get(count);
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String readLine = br.readLine();
                out = new PrintStream(new FileOutputStream("/Users/xyz/Desktop/metaDataFormatted.txt"));
                System.out.println(readLine);
                System.setOut(out);
                //out.flush();
                //out.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("Error During Reading/Writing");
                }
                System.out.println(count+" >");
                //the following line is a method from another class which produces all the output on standard out.
                                    md.getHomePageLinks(originalString);






    }
anonuser0428
  • 11,789
  • 22
  • 63
  • 86
  • 1
    See previously answered question: http://stackoverflow.com/questions/1994255/how-to-write-console-output-to-a-txt-file – redhotspike Sep 12 '12 at 03:35

1 Answers1

2

Try to call System.out.println(data) before you close the stream.

 System.out.println(readLine);
 System.setOut(out);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186