Java 输入输出流03

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); 

      主要方法:     

// 读取一个字符,返回值为读取的字符
public int read () throws IOException;  


// 读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量
public int read (char cbuf []) throws IOException;

// 读取 len 个字符,从数组 cbuf [] 的下标 off 处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现
public abstract int read(char cbuf[],int off,int len) throws IOException; 

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"));      }  
}

 

Reader 类能够将输入流中采用其他编码类型的字符转换为 Unicode 字符,然后在内存中为其分配内存
Writer 类能够将内存中的 Unicode 字符转换为其他编码类型的字符,再写到输出流中。

9. IOException 异常类的子类

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值