字符流和字节流

字符流和字节流及其相关流的继承关系:
字符流和字节流
Reader
常用的方法:
1. close()——————-关闭流并释放与之相关联的任何系统资源
2. int read()—————–读取一个字符并返回,没有读到返回-1
3. int read(char[] cbuf)——–将字符读入数组,并返回读取字符的个数,没有读到返回-1
4. int read(char[] cbuf,int off,int len)-将字符读入数组的一部分
Writer
常用的方法:
1. close()——————-关闭流(先刷新)
2. flush()——————–刷新流
3. void write(char[] cbuf)——-写入一个字符数组
4. void write(char[] cbuf,int off,int len)-写入字符数组的一部分
5. void write(int c)————写一个字符
6. void write(String str)———写一个字符串
7. void write(String str,int off,int len)—写一个字符串的一部分
转换桥梁
InputStreamReader:
从字节流到字符流的桥梁:读取字节,并指定的charset将其解码为字符
Eg:
InputStreamReader isr=new InputStreamReader(new FileInputStream(“u9.txt”));
InputStreamReader isr=new InputStreamReader(new FileInputStream(“u9.txt”),”指定编码”);

OutputStreamWriter:
从字符流到字节流的桥梁:
Eg:
OutputStreamWriter osw=new OutputStream(new FileOutputStreamWriter(“gbk.txt”));
Eg:
OutputStreamWriter osw=new OutputStream(new FileOutputStreamWriter(“gbk.txt”),”指定码表”);
字符流缓冲区:
BufferedReader:
特有方法:
String readLine();—————一次读取一行字符,如果没有读到返回null
BufferedWriter:
特有方法:
newLine();——————–写入一个行分隔符
PrintStream(打印流)
特点:
1.提供了打印方法,可以对多种数据类型进行打印,并保持数据的表示样式
2.他不会抛出IOException
3.构造函数接收3种类型的值
1.字符串路径
2.File对象
3.字节输出流