黑马程序员--Java基础学习笔记【IO流-字节流、转换流】

 

 ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------    

 

  • IO

  • 常用基类

    字节流 InputStreamOutputStream

    常用方法

    int read() // 读取一个字节,以 int 形式返回

intread(byte[] b) // 读取最多数组长度个字节并存入该数组,返回实际读取字节

    void write(int d) // 写出一个字节

voidwrite(byte[] d) // 将给定字节数组中的所有字节全部写出

    void write(byte[] d, int off, int len) // 将字节数组中指定部分写出

 

    字符流 ReaderWriter

    常用方法

    int read() // 读取一个字符,以 int 形式返回

intread(char[] chs) // 读取最多数组长度个字符并存入数组返回读取有效字符

    void wirte(int c) // 写出一个字符

void write(char[]chs) // 将给定字符数组中所有字符写出

    void write(char[] chs, int offset, int len) // 将字符数组中指定部分写出

voidwirte(String str) // 将给定的字符串写出

voidwirte(String str, int off, int len) // 将字符串中指定部分写出

 

  • 文件流

    FileOutputStream  文件的字节输出流,以字节为单位将数据写入文件

    FileOutputStream(File file)

    FileOutputStream(String name)

    FileOutputStream(File file, true) // 追加模式

FileOutputStream(Stringname, true) // 追加模式

 

    FileInputStream  文件的字节输入流,以字节为单位从文件中读取数据

    FileInputStream(File file)

    FileInputStream(String name)

 

  • 缓冲流

    BufferedOutputStream  缓冲字节输出流

先将数据存入缓冲区,当缓冲区已满时,缓冲流会将数据一次性全部写出

// 创建字节输出流对象,封装文本文件

FileOutputStremfos = new FileOutputStream("C:\\buffer.txt");

// 创建缓冲字节输出流对象

BufferedOutputStreambos = new BufferedOutputStream(fos);

// 所有字节被存入缓冲区,等待一次性写出

bos.write("itcast".getBytes());

// 关闭流之前,缓冲输出流会将缓冲区内容一次性写出

bos.close();

flush 方法

void flush()// 清空缓冲区,将缓冲区中的数据强制写出

 

BufferedInputStream  缓冲字节输入流

一次性读取若干字节并存入缓冲区,然后逐一地将字节返回,直到缓冲区中的数据全部读取完毕,会再次读取若干字节从而反复。减少了读取的次数,从而提高了读取效率。

// 创建字节输入流对象,封装文本文件

FileInputStreamfis = new FileInputStream("C:\\buffer.txt");

// 创建缓冲字节输入流对象

BufferedInputStreambis = new BufferedInputStream(fis);

int len = 0;

// 创建缓冲区数组

byte[] b =new byte[1024];

// 将每次读到的有效字节数组转成字符串输出

while((len =bis.read(b)) != -1) {

    System.out.println(new String(b, 0, len));

}

bis.close();

 

  • 四种方式比较复制效率

 

package cn.itcast;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Scanner;

 

/*

 * 字节流复制文件4种方式,效率对比

 * 1.字节流读写单个字节 FileInputStream, FileOutputStream

 * 2.字节流读写字节数组

 * 3.字节流缓冲区读写单个字节 BufferedInputStream, BufferedOutputStream

 * 4.字节流缓冲区读写字节数组

 */

publicclassCopyTest {

 

    publicstaticvoid main(String[] args) {

       Scannersc = new Scanner(System.in);

       System.out.println("请输入源路径");

       Stringsour= sc.nextLine();

       sour.replace("\\", "\\\\");

      

       System.out.println("请输入目标路径");

       Stringdest= sc.nextLine();

       dest.replace("\\", "\\\\");

      

       sc.close();

      

       copy_1(sour, dest); // 用时 127532 ms

       copy_2(sour, dest); // 用时 186 ms

       copy_3(sour, dest); // 用时 220 ms

       copy_4(sour, dest); // 用时 60 ms

      

    }

 

    // 字节流读写单个字节

    publicstaticvoid copy_1(String sour, String dest) {

       FileInputStreamfis= null;

       FileOutputStreamfos= null;

 

       try {

           fis = new FileInputStream(sour);

           fos = new FileOutputStream(dest);

 

           longstart = System.currentTimeMillis();

           intlen = -1;

           while ((len = fis.read()) != -1) {

              fos.write(len);

           }

           longend = System.currentTimeMillis();

           System.out.println("用时 " + (end- start)+ " ms");

 

       }catch(IOException e) {

           e.printStackTrace();

           thrownew RuntimeException("复制失败");

       }finally{

           try {

              if (fos != null) {

                  fos.close();

              }

           }catch(IOException e) {

              e.printStackTrace();

              thrownew RuntimeException("关闭资源失败");

           }finally{

              if (fis != null) {

                  try {

                     fis.close();

                  }catch(IOException e) {

                     e.printStackTrace();

                     thrownew RuntimeException("关闭资源失败");

                  }

              }

           }

       }

 

    }

 

    // 字节流读写字节数组

    publicstaticvoid copy_2(String sour, String dest) {

       FileInputStreamfis= null;

       FileOutputStreamfos= null;

 

       try {

           fis = new FileInputStream(sour);

           fos = new FileOutputStream(dest);

 

           longstart = System.currentTimeMillis();

           intlen = 0;

           byte[] b = newbyte[1024];

           while ((len = fis.read(b)) != -1) {

              fos.write(b, 0, len);

           }

           longend = System.currentTimeMillis();

           System.out.println("用时 " + (end- start)+ " ms");

 

       }catch(IOException e) {

           e.printStackTrace();

           thrownew RuntimeException("复制失败");

       }finally{

           try {

              if (fos != null) {

                  fos.close();

              }

           }catch(IOException e) {

              e.printStackTrace();

              thrownew RuntimeException("关闭资源失败");

           } finally {

              if (fis != null) {

                  try {

                     fis.close();

                  }catch(IOException e) {

                     e.printStackTrace();

                     thrownew RuntimeException("关闭资源失败");

                  }

              }

           }

       }

      

    }

 

    // 字节流缓冲区读写单个字节

    publicstaticvoid copy_3(String sour, String dest) {

       BufferedInputStreambis= null;

       BufferedOutputStreambos= null;

 

       try {

           bis = new BufferedInputStream(new FileInputStream(sour));

           bos = new BufferedOutputStream(new FileOutputStream(dest));

 

           longstart = System.currentTimeMillis();

           intlen = 0;

           while ((len = bis.read()) != -1) {

              bos.write(len);

           }

           longend = System.currentTimeMillis();

           System.out.println("用时 " + (end- start)+ " ms");

 

       }catch(IOException e) {

           e.printStackTrace();

           thrownew RuntimeException("复制失败");

       }finally{

           try {

              if (bos != null) {

                  bos.close();

              }

           }catch(IOException e) {

              e.printStackTrace();

              thrownew RuntimeException("关闭资源失败");

           }finally{

              if (bis != null) {

                  try {

                     bis.close();

                  }catch(IOException e) {

                     e.printStackTrace();

                     thrownew RuntimeException("关闭资源失败");

                  }

              }

           }

       }

 

    }

 

    // 字节流缓冲区读写字节数组

    publicstaticvoid copy_4(String sour, String dest) {

       BufferedInputStreambis= null;

       BufferedOutputStreambos= null;

 

       try {

           bis = new BufferedInputStream(new FileInputStream(sour));

           bos = new BufferedOutputStream(new FileOutputStream(dest));

 

           longstart = System.currentTimeMillis();

           intlen = 0;

           byte[] b = newbyte[1024];

           while ((len = bis.read(b)) != -1) {

              bos.write(b, 0, len);

           }

           longend = System.currentTimeMillis();

           System.out.println("用时 " + (end- start)+ " ms");

 

       }catch(IOException e) {

           e.printStackTrace();

           thrownew RuntimeException("复制失败");

       }finally{

           try {

              if (bos != null) {

                  bos.close();

              }

           }catch(IOException e) {

              e.printStackTrace();

              thrownew RuntimeException("关闭资源失败");

           }finally{

              if (bis != null) {

                  try {

                     bis.close();

                  }catch(IOException e) {

                     e.printStackTrace();

                     thrownew RuntimeException("关闭资源失败");

                  }

              }

           }

       }

 

    }

}

 

转换流

    字符流 = 字节流 + 编码表

    OutputStreamWriter 字符输出流继承 Writer  字符 --> 字节

可以设置字符集,并按照指定的字符集将字符转换为对应字节后通过该流写出

    OutputStreamWriter(OutputStream out)

    OutputStreamWriter(OutputStream out, String charsetName)

 

    FileOutputStream fos = new FileOutputStream("C:\\demo.txt");

    OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");

    String str = "传智播客";

    // 将给定的字符串写出

    osw.write(str);

    osw.flush();

    writer.close();

 

InputStreamReader  字符输入流继承 Reader  字节 --> 字符

可以设置字符集,并按照指定的字符集从流中按照该编码将字节数据转换为字符并读取

    InputStreamReader(InputStreamReader in)

    InputStreamReader(InputStreamReader in, String charsetName)

 

    FileInputStream fis = new FileInputStream(C:\\demo.txt);

    InputStreamReader isr = new InputStreamReader(fis,"utf-8");

    int len = 0;

    char[] cs = new char[1024];

    // 将读取到的有效字符转成字符串输出

    while((len = isr.read()) != -1) {

       System.out.println(new String(cs, 0, len));

    }

转换流的简化写法:

常见的操作都是按照本地默认编码实现的,为了简化书写,转换流提供其对应的子类。FileWriterFileReader (创建对象时可以直接传递字符串格式的文件名)


你可能感兴趣的:(字节流,IO流,转换流)