博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java输入输出流 3
阅读量:7015 次
发布时间:2019-06-28

本文共 3680 字,大约阅读时间需要 12 分钟。

hot3.png

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

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

9. IOException异常类的子类

1.public class  EOFException :

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

转载于:https://my.oschina.net/weiweiqiao/blog/498963

你可能感兴趣的文章
集中管理:领导者,不能不考虑的几件事之—— 拿什么辅助你,我的决策?(一)...
查看>>
四、物理优化(6)数据库引擎优化顾问
查看>>
我的友情链接
查看>>
关于VirtualBox虚拟机安装GhostXP出现蓝屏proce***.sys 的解决办法
查看>>
浅谈 C#委托
查看>>
Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js
查看>>
【Thread】多线程的异常处理?
查看>>
H.264 CODEC
查看>>
计算机图形学中的经常使用模型
查看>>
Android生成keystore是报错拒绝访问
查看>>
JSP如何在servlet将一个数据模型对象传递给jsp页面
查看>>
PHP 实现“贴吧神兽”验证码
查看>>
根据一个表的数据情况显示另一个表的数据
查看>>
Python中的类
查看>>
【摘】请问make -j8 和make -j4 是什么意思?什么作用?
查看>>
iOS 10 的适配问题-b
查看>>
linux设备驱动编写_tasklet机制
查看>>
Mysql客户端中文乱码问题解决
查看>>
分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery
查看>>
课程2:《黑马程序员_Java基础视频-深入浅出精华版》-视频列表-
查看>>