重定向输出流
try {
//一次性创建PrintStream输出流
PrintStream printStream = new PrintStream("D:\\a.txt");
//将标准输出重定向到ps输出流
System.setOut(printStream);
//向标准输出流输出一个字符串
printStream.println("亚麻跌");
printStream.print(new setOut_setIn());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
重定向输入流
使用a.txt文件作为标准输入流
try {
FileInputStream file = new FileInputStream("D:\\a.txt");
//将标准输入重定向到file输入流
System.setIn(file);
//使用System.in创建Sacnner对象,用于标准输入
Scanner sc = new Scanner(System.in);
//增加下面一行只把回车作为分隔符
sc.useDelimiter("\n");
//判断是否还有下一个输入项
while(sc.hasNext()) {
System.out.println("键盘输入内容为:"+sc.next());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}```