从标准输入中读取
package com.zachary.io.standard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Zachary.Zheng
* @version 1.0
* @date 2020年5月30日 下午3:41:47
*/
public class Echo {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("---------------");
while ((s = reader.readLine()) != null && s.length() != 0) {
System.out.println("BufferedReader: " + s);
}
}
}
将System.out转换为PrintWriter
package com.zachary.io.standard;
import java.io.PrintWriter;
/**
* @author Zachary.Zheng
* @version 1.0
* @date 2020年5月30日 下午3:53:00
*/
public class ChangeSystemWriter {
public static void main(String[] args) {
PrintWriter writer = new PrintWriter(System.out, true);
writer.println("ChangeSystemWriter");
}
}
标准I/O重定向
package com.zachary.io.standard;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
/**
* @author Zachary.Zheng
* @version 1.0
* @date 2020年5月30日 下午3:56:49
*/
public class Redirecting {
public static void main(String[] args) throws IOException {
PrintStream console = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream("src/com/zachary/io/standard/Redirecting.java"));
File file = new File("file/output/standard");
file.mkdirs();
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("file/output/standard/test.out")));
System.setIn(in);
System.setOut(out);
System.setErr(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
out.close();
System.setOut(console);
}
}