8. 字符流Writer/Reader
Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。
1. Reader抽象类
用于读取字符流的抽象类。子类必须实现的方法只有 read(char[], int, int) 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。
1) FileReader :与FileInputStream对应
主要用来读取字符文件,使用缺省的字符编码,有三种构造函数: (1)将文件名作为字符串 :FileReader f=new FileReader(“c:/temp.txt”); (2)构造函数将File对象作为其参数。 File f=new file(“c:/temp.txt”); FileReader f1=new FileReader(f); (3) 构造函数将FileDescriptor对象作为参数 FileDescriptor() fd=new FileDescriptor() FileReader f2=new FileReader(fd); (1) 用指定字符数组作为参数:CharArrayReader(char[]) (2) 将字符数组作为输入流:CharArrayReader(char[], int, int) 读取字符串,构造函数如下: public StringReader(String s); 2) CharArrayReader:与ByteArrayInputStream对应 3) StringReader : 与StringBufferInputStream对应 4) InputStreamReader 从输入流读取字节,在将它们转换成字符:Public inputstreamReader(inputstream is); 5) FilterReader: 允许过滤字符流 protected filterReader(Reader r); 6) BufferReader :接受Reader对象作为参数,并对其添加字符缓冲器,使用readline()方法可以读取一行。 Public BufferReader(Reader r);主要方法:
(1) public int read() throws IOException; //读取一个字符,返回值为读取的字符
(2) public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/ (3) public abstract int read(char cbuf[],int off,int len) throws IOException; /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/
2. Writer抽象类
写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。 其子类如下:
1) FileWrite: 与FileOutputStream对应 将字符类型数据写入文件,使用缺省字符编码和缓冲器大小。 Public FileWrite(file f); 2) chararrayWrite:与ByteArrayOutputStream对应 ,将字符缓冲器用作输出。 Public CharArrayWrite(); 3) PrintWrite:生成格式化输出 public PrintWriter(outputstream os); 4) filterWriter:用于写入过滤字符流 protected FilterWriter(Writer w); 5) PipedWriter:与PipedOutputStream对应
6) StringWriter:无与之对应的以字节为导向的stream
主要方法:
(1) public void write(int c) throws IOException; //将整型值c的低16位写入输出流 (2) public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流 (3) public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流 (4) public void write(String str) throws IOException; //将字符串str中的字符写入输出流 (5) public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流 (6) flush( ) //刷空输出流,并输出所有被缓存的字节。 (7)close() 关闭流 public abstract void close() throws IOException
3 .InputStream与Reader差别 OutputStream与Writer差别
InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit)Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题
import java.io.*; public class EncodeTest { private static void readBuff(byte [] buff) throws IOException { ByteArrayInputStream in =new ByteArrayInputStream(buff); int data; while((data=in.read())!=-1) System.out.print(data+" "); System.out.println(); in.close(); } public static void main(String args[]) throws IOException { System.out.println("内存中采用unicode字符编码:" ); char c='好'; int lowBit=c&0xFF; int highBit=(c&0xFF00)>>8; System.out.println(""+lowBit+" "+highBit); String s="好"; System.out.println("本地操作系统默认字符编码:"); readBuff(s.getBytes()); System.out.println("采用GBK字符编码:"); readBuff(s.getBytes("GBK")); System.out.println("采用UTF-8字符编码:"); readBuff(s.getBytes("UTF-8")); } }
9. IOException异常类的子类
1.public class EOFException : 非正常到达文件尾或输入流尾时,抛出这种类型的异常。2.public class FileNotFoundException: 当文件找不到时,抛出的异常。3.public class InterruptedIOException: 当I/O操作被中断时,抛出这种类型的异常。