最近实习,在做DDB(分布式数据库)的导入导出功能
需要对导出的文件从控制台重定向到文件中,所以来复习一下流的重定向功能、
package go.jacob.day1207;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) throws FileNotFoundException {
/**
* 保存最原始的输入输出流
*/
InputStream in = System.in;
PrintStream out = System.out;
/**
* 将标准输入流重定向至 in.txt
*/
System.setIn(new FileInputStream("d:\\test\\in.txt"));
Scanner scanner = new Scanner(System.in);
/**
* 将标准输出流重定向至 out.txt
*/
System.setOut(new PrintStream("d:\\test\\out.txt"));
/**
* 将 in.txt中的数据输出到 out.txt中
*/
while (scanner.hasNextLine()) {
String str = scanner.nextLine();
System.out.println(str);
}
/**
* 将标准输出流重定向至控制台
*/
System.setIn(in);
/**
* 将标准输出流重定向至控制台
*/
System.setOut(out);
scanner = new Scanner(System.in);
String string = scanner.nextLine();
System.out.println("输入输出流已经恢复 " + string);
}
}