标准输出重定向
import java.io.FileOutputStream; import java.io.PrintStream; public class Test { public static void main(String[] args) throws Exception { PrintStream ps=new PrintStream(new FileOutputStream("work")); System.setOut(ps); System.out.println("Hello World!"); } }
标准输入重定向
1 import java.io.FileInputStream; 2 import java.util.Scanner; 3 4 5 public class cdx { 6 public static void main(String[] args) throws Exception 7 { 8 FileInputStream fis=new FileInputStream("work"); 9 System.setIn(fis); 10 11 Scanner sc=new Scanner(System.in); 12 while(sc.hasNextLine()) 13 { 14 System.out.println(sc.nextLine()); 15 } 16 } 17 }