Friday, May 11, 2012

how to redirect the System.out.println to a file

below code will demonstrate , how to redirect System.out.println() , to a file


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class Child {

public static void main(String[] args){

try {

OutputStream output = new FileOutputStream("C:\\ramesh.txt");

PrintStream p = new PrintStream(output);

System.setOut(p);

    Set<Integer> s = new HashSet<Integer>();

s.add(1);
s.add(2);
s.add(3);
s.add(4);
s.add(5);

System.out.println(s);

Set s1 = Collections.unmodifiableSet(s);

//s1.add(6);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

}

No comments:

Post a Comment