A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ls61532529 中级黑马   /  2014-12-4 20:01  /  3336 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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();

评分

参与人数 1黑马币 +3 收起 理由
杨佳名 + 3 赞一个!

查看全部评分

5 个回复

倒序浏览
楼主辛苦了
回复 使用道具 举报
自己动手丰衣足食
回复 使用道具 举报
今天刚看完了!呵呵
回复 使用道具 举报
谢谢楼主
回复 使用道具 举报
可以,前面的集合学习的还是可以的,但是到了io流就有点懵逼了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马