盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

JAVA图文说明教程 第4讲 Java的例外处理与I/O流(二)

时间:2024/11/8作者:未知来源:盾怪网教程人气:

[摘要]+----java.io.FilterInputStream   为了使用一个过滤流,必须首先把过滤流连接到某个输入/出流上,通常通过在构造方法的参数中指定所要连接的输入/出流来实现。例如:   F...

   +----java.io.FilterInputStream



  为了使用一个过滤流,必须首先把过滤流连接到某个输入/出流上,通常通过在构造方法的参数中指定所要连接的输入/出流来实现。例如:

  FilterInputStream( InputStream in );
  FilterOutputStream( OutputStream out );

  4.6.1 几种常见的过滤流

  ◇ BufferedInputStream和BufferedOutputStream
    缓冲流,用于提高输入/输出处理的效率。

  ◇ DataInputStream 和 DataOutputStream
    不仅能读/写数据流,而且能读/写各种的java语言的基本类型,如:boolean,int,float等。

  ◇ LineNumberInputStream
    除了提供对输入处理的支持外,LineNumberInputStream可以记录当前的行号。

  ◇ PushbackInputStream
    提供了一个方法可以把刚读过的字节退回到输入流中,以便重新再读一遍。

  ◇ PrintStream
    打印流的作用是把Java语言的内构类型以其字符表示形式送到相应的输出流。

  4.7 字符流的处理

  java中提供了处理以16位的Unicode码表示的字符流的类,即以Reader和Writer 为基类派生出的一系列类。

  4.7.1 Reader和Writer

   这两个类是抽象类,只是提供了一系列用于字符流处理的接口,不能生成这两个类的实例,只能通过使用由它们派生出来的子类对象来处理字符流。

  1.Reader类是处理所有字符流输入类的父类。

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

  ◇ 标记流
  public boolean markSupported(); //判断当前流是否支持做标记
  public void mark(int readAheadLimit) throws IOException;
   //给当前流作标记,最多支持readAheadLimit个字符的回溯。
  public void reset() throws IOException; //将当前流重置到做标记处

  ◇ 关闭流
  public abstract void close() throws IOException;

  2. Writer类是处理所有字符流输出类的父类。

  ◇ 向输出流写入字符
  public void write(int c) throws IOException;
  //将整型值c的低16位写入输出流
  public void write(char cbuf[]) throws IOException;
  //将字符数组cbuf[]写入输出流
  public abstract void write(char cbuf[],int off,int len) throws IOException;
  //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
  public void write(String str) throws IOException;
  //将字符串str中的字符写入输出流
  public void write(String str,int off,int len) throws IOException;
  //将字符串str 中从索引off开始处的len个字符写入输出流

  ◇ flush( )
  刷空输出流,并输出所有被缓存的字节。

  ◇ 关闭流
  public abstract void close() throws IOException;

  4.7.2 InputStreamReader和OutputStreamWriter

  java.io包中用于处理字符流的最基本的类,用来在字节流和字符流之间作为中介。

   ◇ 生成流对象
  public InputStreamReader(InputStream in);
  /*in是字节流,而InputStreamReader是字符流,但是其来源是字节流in,
  因此InputStreamReader就可以把字节流in转换成字符流处理。/*

  public InputStreamReader(InputStream in,String enc) throws UnsupportedEncodingException;
  /*enc是编码方式,就是从字节流到字符流进行转换时所采用的编码方式,
   例如 ISO8859-1,UTF-8,UTF-16等等*/

  public OutputStreamWriter(OutputStream out);
  /*out是字节流,而OutputStreamReader是字符流 */

  public OutputStreamWriter(OutputStream out,String enc) throws UnsupportedEncodingException; //enc是编码方式

  InputStreamReader和OutputStreamWriter的方法:

  ◇ 读入和写出字符
  基本同Reader和Writer。

  ◇ 获取当前编码方式
  public String getEncoding();

  ◇ 关闭流
  public void close() throws IOException;


关键词:JAVA图文详细教程 第4讲 Java的例外处理与I/O流(二)




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版