1、IO流的分类
输入流 | 输出流 | 说明 | |
---|---|---|---|
字节流 | InputStream | OutputStream | 字节流是处理字节的(二进制) |
字符流 | Reader | Writer | 字符流是处理字符的 |
注:这几个类都是抽象类
2、读文件的代码
import java.io.FileInputStream;
import java.io.IOException;
/**
* Created by SunMing on 2016/9/19.
*/
public class ReadFile {
public static void main(String[] args) {
String path = "c://a.txt";
FileInputStream in = null;
try {
// 打开流
in = new FileInputStream(path);
// 使用流读文件内容
int b = in.read();
while (b != -1) {
System.out.print((char) b);
b = in.read();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 释放资源
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}