How to redirect System.out output to a file, instead of Console

You can read about standard input ans standard output stream details here. As known System.out represents Standard output stream, and anything written to standard output stream, gets displayed on console screen. How ever it is possible to redirect output of System.out to any OutputStream, like FileOutputStream, or to Network. Below is an example, on how to redirect the content written to System.out to a file name abcd.txt.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class SystemIORedirectDemo {
public static void main(String[] args) {
	try{
	FileOutputStream fos = new FileOutputStream("abcd.txt");
	PrintStream ps = new PrintStream(fos);
	System.setOut(ps);
	
	System.out.println("Hello, How are you");
	System.out.println("Note that this content is saved to abcd.txt file");
	}
	catch(IOException ioe)
	{
		System.out.println("IOException occurred:"+ioe.getMessage());
	}
}
}

By default, System.in reads each character from Keyboard. Below is an example on how to redirect System.in. Similarly it is also possible to redirect System.err

import java.io.FileInputStream;
import java.io.IOException;

public class SystemIORedirect1Demo {
public static void main(String[] args) throws IOException{
	FileInputStream fis = new FileInputStream("abcd.txt");
	
	System.setIn(fis);
	
	int i = 0;
	while(i != -1)
	{
	i = System.in.read();//reads each character from the file abcd.txt
	System.out.print((char)i);//prints the character read, above
	}
}
}

You may also like to read:

java standard input and output

Program to print non English characters in Java

You might wonder, how to print Non English characters using Java.

Below is program, which initializes a String literal, using Hexa decimal notation, which has non english characters.

public class multiplyu {
public static void main(String[]args){
	String str = "u0905u0906u0907u0908u0909u0910u0911";
	System.out.println("Non-English Characters:"+str);
}
}

In above program, 0905, 0906, 0907, 0908, 0909, 0910 and 0911 are Hexadecimal representation of Hindi characters. Each of these hexa decimal code need to be prefixed with u to indicate that these are hex literals.

Similarly any Unicode characters can be initialized to String object, and can be printed.

Below is output screen shot:

Print non English characters in Java
Print non English characters in Java

In Eclipse, by default Work space Encoding is Cp1252, which can print only English characters in Console.

Below steps can be used to print non-English characters in Eclipse console.
1.Select Window
2.Select Preferences
3.then Select, WorkSpace
4.On right section, select ‘Text File Encoding’ as UTF-8

Print Non Ascii characters  using Java
Print Non Ascii characters using Java