0

I am trying to redirect the output produced in a Java program into another file("output.txt") located in the same location. I found help on it from here. But when I try the same method I find that my file "output.txt" remains empty. My code looks like this:

System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.out.println("hello");

Could somebody please point out my mistake here. Thank you in advance for your help.

Goldengirl
  • 957
  • 4
  • 10
  • 30
  • Could somebody tell me why has it been down voted??? – Goldengirl Aug 14 '15 at 11:41
  • Where is the reason-option about "demonstrate a minimal understanding"? – Andremoniy Aug 14 '15 at 11:41
  • It was downvoted cause you are comparing apples and pears – Andremoniy Aug 14 '15 at 11:43
  • @Andremoniy : I am sorry but do you mean that my question makes no sense to you? And I dont understand what you mean by Apples and pears here.. – Goldengirl Aug 14 '15 at 11:43
  • @Goldengirl can you describe what you are trying to achieve here? do you want to dynamically create a java source code? Or log messages into another file? – Brainhash Aug 14 '15 at 11:50
  • @Brainhash : What I want to do is just print the output(for example "hello" ) into a output.txt file from a Java program. So this code that I have posted above is part of my main Java program. – Goldengirl Aug 14 '15 at 11:54
  • Your code works as is for me. I ended up with hello in output.txt. Maybe you need to reopen file or your looking in the wrong directory. – WillShackleford Aug 14 '15 at 12:09
  • @WillShackleford: I dont know what is wrong when I do it? Nothing seems to be happening in the output.txt file.. – Goldengirl Aug 14 '15 at 12:12
  • It works for me too. Are you checking for file in same location where you are running program? – Ravindra babu Aug 14 '15 at 14:03

1 Answers1

0

As it is said in this answer, you should try to set autoflushing to true

Community
  • 1
  • 1
S K
  • 170
  • 8