黑马程序员_IO流

来源:互联网 发布:阿里云 自建服务器 编辑:程序博客网 时间:2024/04/29 23:37

------- android培训、java培训、期待与您交流! ----------

1.什么叫流
流就是程序和设备之间嫁接起来的一根用于数据传输的管道。

2.流的分类
      java.io包中定义了多个流类型来实现输入/输出功能;可以从不同的角度对其进行分类:
      按数据流的方向不同可以分为输入流和输出流。
      按处理数据单位不同可以分为字节流和字符流。
      按功能不同可以分为节点流和处理流。

3.四大基本抽象流
    四大基本抽象类:InputStream、OutputStream、Reader、Writer。通常我们使用的都是它们的子类。
    凡是以stream结尾的都是字节流。reader和writer都是字符流。
 
3.1InputStream流中常用的方法
   public  int read()            throws IOException
   ·读取一个字节并以整数形式返回
   ·如果读取到输入流的末尾则返回-1

   public int read(byte[] b)  thorws IOException
   ·从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。以整数形式返回实际读取的字节数
   ·如果b的长度为0,则不读取任何字节并返回0;如果因为位于文件末尾而 没有可用的字节,则返回值-1

3.2 OutputStream流中常用的方法
   void write(int b) throws IOException
   ·向输出流中写入一个字节数据,该字节参数为b的第八位
  
    void write(byte[] b)throws  IOException
   ·将一个字节类型的数组数据写入输入流

    void write (byte [] b,int off,int len)throws IOException
   ·将一个字节类型的数组中的从指定位置(off)开始的,len个字节写入输入流
  
    void close() throws IOException
   ·关闭释放内存资源

    void flush() throws IOException
   ·将输出流中缓冲的数据全部写出到目的地
  
3.3 Reader流的常用方法
   int  read() throws IOException
   ·读取一个字符并以整数的形式返回(0 ~ 255),若返回-1已到输入流的末尾
  
   int read(char[] buf)throws IOException
   ·读取一系列字符并存储到一个数组buf,到末尾时返回-1
 
   int read (char [] buf,int offset,int length) throws IOException
   ·最多读取length个字符并存储到一个数组buffer,从length位置开始

   void close() throws IOException  
   ·关闭释放内存资源
   
   long skip(long n) throws IOException
   ·跳过n个字符不读,返回实际跳过的字节数 

3.4 Writer流中常用的方法
    void write(int c) throws IOException
    ·向输出流中写入一个字符数据,该字节数据为参数b的低16位

    void write(char[] buf) throws IOException
    ·将一个字符类型的数组中的数据写入输出流
   
    void write(char[] buf,int offset,int length)throws IOException
    ·将一个字符类型的数组从指定位置(offset)开始的length个字符写入输出流

    void write(String string) throws IOException
    ·将一个字符串中的字符写入输出流

    void write(String string,int offset,int length) throws IOException
    ·将一个字符串从offset开始的length个字符写入输出流

    void close() throws IOException
    ·关闭释放内存资源
   
    void flush() throws IOException
    ·将输出流缓冲的数据全部写到目的地

4. 文件流
   文件流包括
   ·FileInputStream  FileOutputStream      ---字节流
   ·FIleReader    FileWriter               ---字符流

4.1 字符流输出文件
     public  class TestIO{
        public static void main(String[] args)throws Exception{
           FileReader fr=new FileReader("d/1.txt");
           FileWriter fw=new FileWriter("d/2.txt");
           int ch;
           
           ch=fr.read();
           while(-1!=ch){
           ch=fr.read();
        }
        fw.flush();
        fr.close();
        fr.close();
}
      字符流是用来读取字符的。
 
4.2 字节流输出文件
   public class TestIO{
    public  static void  main(String [] args)throws Exception{
    FileInputStream fr=new FileInputStream(d:/1.mp3);
    FileOutputStream fw=new FileOutputStream(d:/2.mp3);
    int ch;
   
    ch=fr.read();
    while(-1!=ch){
    fw.write(ch);
    ch=fr.read();
    }
    fw.flush();
    fr.close();
    fw.close();
}
   字节流是用来读取字节的。

4.3 字节流和字符流的区别
   ·InputStream和FileOutputStreamStream可以完成所有格式的赋值
   ·FileReader和FileWiter只可以完成文本的复制,却无法完成视屏文件的赋值
   ·字节流可以从所有文件格式的设备中读写数据,但字符流只能从文本格式设备中读写数据
   ·因为字节是不需要解码和编码的,将字节转化字符才存在解码和读码问题

5.缓冲流

5.1缓冲流的概述
  ·缓冲流就是带有缓冲区的输入输出流
  ·缓冲流可以显著地减少我们对IO的访问次数,保护我们的硬盘!
  ·缓冲流本身就是处理,缓冲流必须得依附于节点流
  ·处理流是包裹在原始节点流上的流,相当于包括在管道上的管道

5.2 BufferedReader和BufferedWriter
     public  static void main(String[] args)throws Exception{
      BufferedInputStream bis=
          new BufferedInputStream(
               new FileInputStream("d:/11.mp3");
      BufferedOutputStream bos=
          new BufferedOutputStream(
               new FileOutputStream("d:/22.mp3");
      len[] buf=new byte[1024];
      while(-1!=len){
        bos.write(buf);
        len=bis.read(buf,0,len);
        }
        bos.flush();
        
        bos.close();
        bis.close();
  }
}

带缓冲流的处理文件的速度要快于不带缓冲区的字节流

 

------- android培训、java培训、期待与您交流! ----------