user2916888
user2916888

Reputation: 83

How to reset standard input and output in java?

I have following java code which redirects standard input and output to files:

FileInputStream fin=new FileInputStream("In.txt");
System.setIn(fin); //Standard input will be taken from file In.txt

FileOutputStream fout=new FileOutputStream("Out.txt");
PrintStream p=new PrintStream(fout);
System.setOut(p); //Standard output of the program will be written to file Out.txt

Now after performing necessory operations I want to reset the IO. i.e. the program should again accept input from keyboard and print output on the screen. Is there any code for that? Please help

Upvotes: 2

Views: 1602

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

What about this

InputStream in = System.in;
System.setIn(something else);
// do something

System.setIn(in);

Messing about with System.in and System.out is a serious hack which is unlikely to work quite the way you want. You are better of finding another way to read one file and write to another.

Upvotes: 3

Related Questions