加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java I/O数据流 (4)

(2013-05-27 21:20:54)
分类: 转载

8. 字符流Writer/Reader

        Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。

 

1. Reader抽象类

 

    用于读取字符流的抽象类。子类必须实现的方法只有 read(char[], int, int) 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

       http://my.csdn.net/uploads/201204/01/1333250341_6152.gifI/O数据流 (4)" />

        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;  
  (3)  public abstract int read(char cbuf[],int off,int len) throws IOException; 
   

2. Writer抽象类

     写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。 其子类如下:

    http://my.csdn.net/uploads/201204/01/1333250133_8675.gifI/O数据流 (4)" />

      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处理的是字符流,在处理字符流时涉及了字符编码的转换问题

 

  1. import java.io.*;  
  2. public class EncodeTest  
  3.     private static void readBuff(byte [] buff) throws IOException  
  4.        ByteArrayInputStream in =new ByteArrayInputStream(buff);  
  5.         int data;  
  6.         while((data=in.read())!=-1  System.out.print(data+ ");  
  7.         System.out.println();     in.close();      
  8.   
  9.    public static void main(String args[]) throws IOException  
  10.        System.out.println("内存中采用unicode字符编码:" );  
  11.        char   c='好' 
  12.        int lowBit=c&0xFF    int highBit=(c&0xFF00)>>8 
  13.        System.out.println(""+lowBit+  "+highBit);  
  14.        String s="好" 
  15.        System.out.println("本地操作系统默认字符编码:");  
  16.        readBuff(s.getBytes());  
  17.        System.out.println("采用GBK字符编码:");  
  18.        readBuff(s.getBytes("GBK"));  
  19.        System.out.println("采用UTF-8字符编码:");        
  20.        readBuff(s.getBytes("UTF-8"));       
  21.  
http://img.my.csdn.net/uploads/201211/29/1354191297_8218.jpgI/O数据流 (4)" />
Reader类能够将输入流中采用其他编码类型的字符转换为Unicode字符,然后在内存中为其分配内存
Writer类能够将内存中的Unicode字符转换为其他编码类型的字符,再写到输出流中。

 

9. IOException异常类的子类

 

1.public class  EOFException :
   非正常到达文件尾或输入流尾时,抛出这种类型的异常。
2.public class FileNotFoundException:
   当文件找不到时,抛出的异常。
3.public class InterruptedIOException:
   当I/O操作被中断时,抛出这种类型的异常。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有