Java IO类详解(三)

 三. OutputStream类,InputStream类,Reader类,Writer类

       在程序中所有的数据都是以流的方式进行传输或者保存的,在Java.io中主要有字节流和字符流两大类:字节流--->OutputStream类,InputStream类;字符流--->Reader类,Writer类。并且这四个类都是抽象类,必须通过子类才能实例化。

       在Java中IO操作主要有以下几步;

        1.使用File类打开一个文件;

        2.通过字节流或字符流的子类指定输入输出的位置;

        3.进行读/写操作;

        4.关闭输入/输出。

 

@1  字节流

        下面是字节流类及其子类关系:

        InputStream

 

42134.gif

     InputStream类的主要方法:

     InputStream类的定义:

     ===

     public abstract class InputStream extends Object implaments Closeable,Flushable

     ===

     构造方法:public InputStream();

     普通方法:public int available() throws IOException                     //可以取得输入文件的大小

                      public void close() throws IOException                          //关闭输入流

                      public abstract int read() throws IOException               //读取内容从输入流中读取数据的下一个字节

                      public abstract int read(byte[] b) throws IOException    //从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b

                      public abstract int read(byte[] b,int off,int len) throws IOException //将输入流中最多 len 个数据字节读入 byte 数组

                      public long skip(long n) throws IOException                 //跳过和丢弃此输入流中数据的 n 个字节

                      public void mark(int readlimit) throws IOException    //在此输入流中标记当前的位置

                      public void reset() throws IOException                          //将此流重新定位到最后一次对此输入流调用 mark 方法时的位置

                      public boolean markSupported() throws IOException //测试此输入流是否支持 markreset 方法

 

     下面是InputStream类的子类详解:

      1.FileInputStream类     从文件系统中的某个文件中获得输入字节

       构造函数:public FileInputStream(File f) throws FileNotFoundException

                    public FileInputStream(String filepath) throws FileNotFoundException

       其方法主要继承或覆盖InputStream类的方法。

     2.ByteArrayInputStream  内存操作流,它包含一个内部缓冲区,该缓冲区包含从流中读取的字节。

        构造函数:public ByteArrayInputStream(byte[] b)

                     public ByteArrayInputStream(byte[] b,int off,int len)

        其方法主要继承或覆盖InputStream类的方法。

     3.PipedInputStream  管道操作流的主要作用是可以进行两个线程间的通信。

        构造函数:public PipedInputStream()

        普通方法:public void connect(PipedOutputStream pos)  throws IOException //使此管道输入流连接到管道输出流 pos   这个是管道流里最重要的方法,也是关键的方法。使用它才可以实现管道流就收传送数据。

                     protected void receive(int b) throws IOException  //接收数据字节

     4.PrintStream 字节打印流。打印流是输出信息最方便的类,主要包括字节打印流PrintStream和字符打印流PrintWrite

        =====

        定义:public class PrintStream extends FileOutputStream implementd Appendable,Closeable

        =====

        构造:public PrintStream(File file) throws FileNotFoundException //

                public PrintStream(OutputStream out) //

                public PrintStream(String filename) throws FileNotFoundException //

        普通:

PrintStreamappend(char c)
          将指定字符添加到此输出流。
 PrintStreamappend(CharSequence csq)
          将指定字符序列添加到此输出流。
 PrintStreamappend(CharSequence csq, int start, int end)
          将指定字符序列的子序列添加到此输出流。
 booleancheckError()
          刷新流并检查其错误状态。
protected  voidclearError()
          清除此流的内部错误状态。
 voidclose()
          关闭流。
 voidflush()
          刷新该流的缓冲。
 PrintStreamformat(Locale l, String format, Object... args)
          使用指定格式字符串和参数将格式化字符串写入此输出流中。
 PrintStreamformat(String format, Object... args)
          使用指定格式字符串和参数将格式化字符串写入此输出流中。
 voidprint(boolean b)
          打印 boolean 值。
 voidprint(char c)
          打印字符。
 voidprint(char[] s)
          打印字符数组。
 voidprint(double d)
          打印双精度浮点数。
 voidprint(float f)
          打印浮点数。
 voidprint(int i)
          打印整数。
 voidprint(long l)
          打印 long 整数。
 voidprint(Object obj)
          打印对象。
 voidprint(String s)
          打印字符串。
 PrintStreamprintf(Locale l, String format, Object... args)
          使用指定格式字符串和参数将格式化的字符串写入此输出流的便捷方法。
 PrintStreamprintf(String format, Object... args)
          使用指定格式字符串和参数将格式化的字符串写入此输出流的便捷方法。
 voidprintln()
          通过写入行分隔符字符串终止当前行。
 voidprintln(boolean x)
          打印 boolean 值,然后终止行。
 voidprintln(char x)
          打印字符,然后终止该行。
 voidprintln(char[] x)
          打印字符数组,然后终止该行。
 voidprintln(double x)
          打印 double,然后终止该行。
 voidprintln(float x)
          打印 float,然后终止该行。
 voidprintln(int x)
          打印整数,然后终止该行。
 voidprintln(long x)
          打印 long,然后终止该行。
 voidprintln(Object x)
          打印 Object,然后终止该行。
 voidprintln(String x)
          打印 String,然后终止该行。
protected  voidsetError()
          将该流的错误状态设置为 true
 voidwrite(byte[] buf, int off, int len)
          将 len 字节从指定的初始偏移量为 off 的 byte 数组写入此流。
 voidwrite(int b)
          将指定的字节写入此流

     OutputSteam

 

42135.gif

 

    OutputStream类的主要方法:

     OutputStream类的定义:

     ===

     public abstract class OutputStream extends Object implaments Closeable,Flushable

     ===

     构造方法:public OutputStream();

     普通方法:public abstract void write(int b) throws IOException       //将一个字节数据写入数据流

                      public  void write(byte[] b) throws IOException                 //将一个字节数组写入数据流

                      public  void write(byte[] b,int off,int len) throws IOException //将一个指定范围内的byte数组写入数据流

                      public  void flush() throws IOException                                 //刷新缓冲区

                      public void close() throws IOeception                                   // 关闭输出流

以上就是OutputStream,InputStream类的主要方法。

 

import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class OutputInputStreamDemo {
 public static void main(String[] args) throws IOException{
  File f=new File("F://myjava//china.txt");
  if(!f.exists()){
   try{
    f.createNewFile();
   }catch(IOException e){
    e.printStackTrace();
   }
  }
  
  OutputStream out=new FileOutputStream(f,true);
  String[] str={"hello/r/n","welcome everyone/r/n","i love you/r/n"};
  for(int i=0;i<str.length;i++){
   byte[] b=str[i].getBytes();
   out.write(b);
  }
  
  out.close();
  
  //
  InputStream in=new FileInputStream(f);
  byte[] content=new byte[(int)f.length()];
  for(int i=0;i<content.length;i++){
   content[i]=(byte)in.read();
  }
  String show=new String(content);
  System.out.println(f.getName()+":"+show);
 }

}

 

 

===========================

===========================

@2 字符流

  Reader

 

42136.gif

 

   Reader类的定义,构造方法以及方法

    Reader类的定义:

    public abstract class Reader extends Object implements Closeable,Flushable

    构造方法:protected Reader()

    普通方法:public int read( ) throws IOException  //读取单个字符

                      public int read(char[] ch) throws IOException //将字符读入数组

                      public abstract int read(char[] ch,int off,int len) throws IOException //将字符读入数组的某一部分

                      public void skip(long n) throws IOException //跳过字符

                      public boolean ready() throws IOexception //判断是否准备读取此流

                      public abstract void close() throws IOException //关闭该流并释放与之关联的所有资源

 

  Writer

 42137.gif

   Writer类的定义,构造方法以及方法

    Writer类的定义:

    public abstract class Writer extends Object implements Closeable,Flushable

    构造方法:protected Writer()

    普通方法:public Writer append(char c) throws IOException   //将指定字符添加到此 writer

                     public void close() throws IOException //  关闭此流,但要先刷新它。

                     public void flush() throws IOException //  刷新该流的缓冲。

                     public void write(char[] ch) throws IOException // 写入字符数组

                     public abstract void write(char[] ch,int off,int len) throws IOException // 写入字符数组的某一部分

                     public void write(int c) throws IOException // 写入单个字符

                     public void write(String str) throws IOException // 写入字符串

                     public void write(String str,int off,int len) throws IOException // 写入字符串的某一部分

 

 

 import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.io.Reader;
import java.io.FileWriter;
import java.io.FileReader;

public class WriterReaderDemo {
 public static void main(String[] args) throws IOException{
  File f=new File("F://myjava//usa.txt");
  if(!f.exists()){
   try{
    f.createNewFile();
   }catch(IOException e){
    e.printStackTrace();
   }
  }
  
  Writer out=new FileWriter(f,true);
  String s="hello/r/n";
  String ss="ok,gogo/r/n";
  char[] ch=new char[]{'a','p','p','e','n','d'};
  out.write(s);
  out.write(ss);
  out.write(ch);
  
  out.close();
  
  
  Reader in=new FileReader(f);
  char[] c=new char[1024];
  //char[] cha=new char[1024];
  //int temp=0,len=0;
  in.read(c);
  String str=new String(c);
  System.out.println(f.getName()+":/r/n"+str);
  
  /*
  while((temp=in.read())!=-1){
  cha[len]=(char)temp;
  len++;
  }
  
  
  System.out.println("/r/n abc:"+new String(cha,0,len));
  
  */
  
  in.close();
  
 }

}

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值