IO流中的重要方法

IO
      (1) 分类
             字节流
                   输入流:
                        InputStream( 抽象类)
                              intread()
                              intread(byte[] bys)
                        FileInputStream( 常用基本流)
                        BufferedInputStream
                   输出流:
                        OutputStream( 抽象类)
                              write(intby)
                              write(byte[]bys,int index,int len)
                        FileOutputStream( 常用基本流)
                        BufferedOutputStream
             字符流:
                   输入流:
                        Reader( 抽象类)
                              intread()
                              intread(char[] chs)
                        FileReader( 常用基本流)
                        BufferedReader
                              String readLine()
                   输出流:
                        Writer( 抽象类)
                              write(intch)
                              write(char[]chs,int index,int len)
                        FileWriter( 常用基本流)
                        BufferedWriter
                              write(String line)
                              void newLine()
      (2) 到底使用谁?
             用记事本打开能读懂的,就用字符流。
             否则,用字节流。
             如果你根本就不知道,用字节流。
      (3) 复制文本文件(了解):
            9 种方式:
                   字节流:
                        4
                               基本流一次读写一个字节
                               基本流一次读写一个字节数组
                               高效流一次读写一个字节
                               高效流一次读写一个字节数组
                   字符流:
                        5
                               基本流一次读写一个字符
                               基本流一次读写一个字符数组
                               高效流一次读写一个字符
                               高效流一次读写一个字符数组
                               高效流一次读写一个字符串
             一般来说,明明知道是文本文件,那么,肯定不用字节流。
             那么,如果是使用字符流,有5 种方式,选择哪一种呢?
             一般都选择高效流读写一个字符串的方式。
             代码体现:
                   数据源: c:\\a.txt
                   目的地: d:\\b.txt
                  BufferedReader br = new BufferedReader(newFileReader("c:\\a.txt"));
                  BufferedWriter bw = new BufferedWriter(newFileWriter("d:\\b.txt"));
                  String line = null;
                  while((line=br.readLine())!=null)
                  {
                        bw.write(line);
                        bw.newLine();
                        bw.flush();
                  }
                  bw.close();
                  br.close();
      (4) 复制二进制流数据:( 图片,视频,音频等)
             字节流:
                        4
                               基本流一次读写一个字节
                               基本流一次读写一个字节数组
                               高效流一次读写一个字节
                               高效流一次读写一个字节数组
             一般来说,我们选择使用高效流一次读写一个字节数组
             代码体现:
                   数据源: c:\\a.jpg
                   目的地: d:\\b.jpg
                  BufferedInputStream bis = new BufferedInputStream(newFileInputStream("c:\\a.jpg"));
                  BufferedOutputStream bos = newBufferedOutputStream(new FileOutputStream("d:\\b.jpg"));
                  byte[] bys = new byte[1024];
                  int len = 0;
                  while((len=bis.read(bys))!=-1)
                  {
                        bos.write(bys,0,len);
                  }
                  bos.close();
                  bis.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值