package iodemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RedirectIn {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:\\a\\out1.txt");
System.setIn(fis);
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");//只把回车作为分隔符
while(scan.hasNext()) {
System.out.println(scan.next());
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RedirectIn {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:\\a\\out1.txt");
System.setIn(fis);
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");//只把回车作为分隔符
while(scan.hasNext()) {
System.out.println(scan.next());
}
}
}
————————————————————————————————————————————————————
package iodemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class RedirectDemo {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream(new File("d:\\a\\out2.txt"));
System.setOut(ps); //输出重定向
System.out.println("重定向到你哦");
}
}