1. 字节流
字节流包含: bytearray filter(过滤器流) object inputstream
package com.io.reader;
import java.io.*;
public class FileReader {
public static String read(String path) throws Exception {
// bufferReader -> 管道字符流(字符输入流(文件子节输入流))
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),"gbk"));
String s = null;
StringBuffer sb = new StringBuffer();
while ( (s = reader.readLine()) != null) {
sb.append(s + "\n");
}
reader.close();
return sb.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(read("D:/BaiduNetdiskDownload/(关注).txt"));
}
}
2. 标准输入输出流
标准输入输出流包括 system.in system.out 以及 system.err
如下代码 : 是进行标准输入输出流的重定向操作案例
package com.io.biaozhunliu;
import java.io.*;
public class SystemOutRedireact {
public static void main(String[] args) throws Exception {
// 记录原始输入输出流
PrintStream console = System.out;
InputStream in = System.in;
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:/BaiduNetdiskDownload/(关注).txt"));
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("D:/haha.txt")));
// 标准输入输出流重定向(将源头重定向)
System.setIn(inputStream);
System.setOut(out);
System.setErr(out);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in,"gbk"));
String s = null;
while ((s = bufferedReader.readLine()) != null) {
System.out.println(s);
}
out.close();
// 还原原始输入输出流
System.setIn(in);
System.setOut(console);
}
}