(八)、Java复习笔记之 I/O 流(2)

来源:互联网 发布:长沙软件学校 编辑:程序博客网 时间:2024/05/18 22:11

I/O流体系图:

这里写图片描述

86、IO流

  • 概念
    • IO流用来处理设备之间的数据传输
    • Java对数据的操作是通过流的方式
    • Java用于操作流的类都在IO包中
    • 流按流向分为两种:输入流,输出流。
    • 流按操作类型分为两种:
      • 字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
      • 字符流 : 字符流只能操作纯字符数据,比较方便。
  • IO流常用父类
    • 字节流的抽象父类:
      • InputStream
      • OutputStream
    • 字符流的抽象父类:
      • Reader
      • Writer
  • IO程序书写
    • 使用前,导入IO包中的类
    • 使用时,进行IO异常处理
    • 使用后,释放资源

87、字节流FileInputStream

  • read()一次读取一个字节
  • FileInputStream fis = new FileInputStream("aaa.txt");   //创建一个文件输入流对象,并关联aaa.txtint b;  //定义变量,记录每次读到的字节while((b = fis.read()) != -1) { //将每次读到的字节赋值给b并判断是否是-1    System.out.println(b);  //打印每一个字节}fis.close(); //关闭流释放资源
  • read()方法读取的是一个字节,为什么返回是int,而不是byte?

    因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111,那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上,24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样可以保证整个数据读完,而结束标记的-1就是int类型

88、字节流FileOutputStream

  • write()一次写出一个字节

    FileOutputStream fos = new FileOutputStream("bbb.txt"); //如果没有bbb.txt,会创建出一个,有就清空其内容//fos.write(97);    //虽然写出的是一个int数,但是在写出的时候会将前面的24个0去掉,所以写出的一个bytefos.write(98);fos.write(99);fos.close();
  • FileOutputStream追加

    FileOutputStream fos = new FileOutputStream("bbb.txt",true); //如果没有bbb.txt,会创建出一个,有就向其追加内容//fos.write(97);                        fos.write(98);fos.write(99);fos.close();

89、拷贝视频

  • 方法一

    • FileInputStream读取
    • FileOutputStream写出
    • 弊端:一次读写一个字节,效率太慢

      FileInputStream fis = new FileInputStream("致青春.mp3"); //创建输入流对象,关联致青春.mp3FileOutputStream fos = new FileOutputStream("copy.mp3"); //创建输出流对象,关联copy.mp3int b;while((b = fis.read()) != -1) {    fos.write(b);}fis.close();fos.close();        
  • 方法二

    • int read(byte[] b):一次读取一个字节数组
    • write(byte[] b):一次写出一个字节数组
    • available():获取读的文件所有的字节个数
    • 弊端:有可能会内存溢出

      FileInputStream fis = new FileInputStream("致青春.mp3");FileOutputStream fos = new FileOutputStream("copy.mp3");byte[] arr = new byte[fis.available()]; //根据文件大小做一个字节数组fis.read(arr);  //将文件上的所有字节读取到数组中fos.write(arr); //将数组中的所有字节一次写到了文件上fis.close();fos.close();
  • 方法三:

    • 字节流一次读写一个字节数组复制视频

      FileInputStream fis = new FileInputStream("致青春.mp3");FileOutputStream fos = new FileOutputStream("copy.mp3");int len;byte[] arr = new byte[1024 * 8];    //自定义字节数组while((len = fis.read(arr)) != -1) {    //fos.write(arr);    fos.write(arr, 0, len); //写出字节数组写出有效个字节个数}fis.close();fos.close();
  • 方法四:
    • BufferedInputStream和BufferOutputStream拷贝。
    • 方法三比方法四稍微快一点。因为方法三读和写操作的是同一个数组,而Buffered操作的是两个数组。

90、BufferedInputStream和BufferOutputStream

  • 缓冲思想
    • 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
    • 这是加入了数组这样的缓冲区效果,java本身在设计的时候,
    • 也考虑到了这样的设计思想(装饰设计模式),所以提供了字节缓冲区流。
  • BufferedInputStream
    • BufferedInputStream内置了一个缓冲区(数组)
    • 程序从BufferedInputStream中读取一个字节时
    • BufferedInputStream会一次性从文件中读取8192个字节, 存在缓冲区中, 返回给程序一个字节
    • 程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
    • 直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个字节
  • BufferedOutputStream
    • BufferedOutputStream也内置了一个缓冲区(数组)
    • 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
    • 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
  • 拷贝的代码

    FileInputStream fis = new FileInputStream("致青春.mp3"); //创建文件输入流对象,关联致青春.mp3BufferedInputStream bis = new BufferedInputStream(fis);  //创建缓冲区对fis装饰FileOutputStream fos = new FileOutputStream("copy.mp3"); //创建输出流对象,关联copy.mp3BufferedOutputStream bos = new BufferedOutputStream(fos); //创建缓冲区对fos装饰int b;while((b = bis.read()) != -1) {         bos.write(b);}bis.close();    //只关装饰后的对象即可bos.close();    

91、flush和close方法的区别

  • flush()方法
    • 用来刷新缓冲区的,刷新后可以再次写出。(实时聊天用的是flush()方法)
  • close()方法
    • 用来关闭流释放资源的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出

92、流的标准处理异常代码

  • 1.6版本及其以前(try finally嵌套)

    FileInputStream fis = null;FileOutputStream fos = null;try {    fis = new FileInputStream("aaa.txt");    fos = new FileOutputStream("bbb.txt");    int b;    while((b = fis.read()) != -1) {        fos.write(b);    }} finally {    try {        if(fis != null)            fis.close();    }finally {        if(fos != null)            fos.close();    }}
  • 1.7版本(try close)
  • 原理:在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用流对象的close方法将流关掉

    try(    FileInputStream fis = new FileInputStream("aaa.txt");    FileOutputStream fos = new FileOutputStream("bbb.txt");    MyClose mc = new MyClose(); //我关了){    int b;    while((b = fis.read()) != -1) {        fos.write(b);    }}class MyClose implements AutoCloseable {    public void close() {        System.out.println("我关了");    }}

93、字节流实例

  • 图片加密

    • 原理:一个数据对另一个数据位异或两次,该数本身不变。

      //加密BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.jpg"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg"));int b;while((b = bis.read()) != -1) {    bos.write(b ^ 123);}bis.close();bos.close();        //解密BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.jpg"));int b;while((b = bis.read()) != -1) {    bos.write(b ^ 123);}bis.close();bos.close();
  • 拷贝文件

    • 在控制台录入文件的路径,将文件拷贝到当前项目下

      Scanner sc = new Scanner(System.in);System.out.println("请输入一个文件路径");String line = sc.nextLine();    /将键盘录入的文件路径存储在line中File file = new File(line);     //封装成File对象FileInputStream fis = new FileInputStream(file);FileOutputStream fos = new FileOutputStream(file.getName());int len;byte[] arr = new byte[8192];                //定义缓冲区while((len = fis.read(arr)) != -1) {    fos.write(arr,0,len);}fis.close();fos.close();        
  • 录入数据拷贝到文件

    • 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出

      Scanner sc = new Scanner(System.in);FileOutputStream fos = new FileOutputStream("text.txt");System.out.println("请输入:");while(true) {    String line = sc.nextLine();    if("quit".equals(line))        break;    fos.write(line.getBytes());    fos.write("\r\n".getBytes());}fos.close();    

94、字符流FileReader、FileWriter

  • 概述
    • 字符流是可以直接读写字符的IO流
    • 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出
  • 实例

    • FileReader类的read()方法按照字符大小(2个字节)读取

      FileReader fr = new FileReader("aaa.txt");              int ch;while((ch = fr.read()) != -1) {                             System.out.println((char)ch); //将读到的字符强转后打印}fr.close();                                                     
    • FileWriter类的write()方法可以自动把字符转为字节写出

      FileWriter fw = new FileWriter("aaa.txt");fw.write("aaa");fw.close(); 
    • 字符流的拷贝

      FileReader fr = new FileReader("a.txt");FileWriter fw = new FileWriter("b.txt");int ch;while((ch = fr.read()) != -1) {    fw.write(ch);}fr.close();fw.close(); 

95、什么情况下使用字符流

  • 字符流也可以拷贝文本文件, 但不推荐使用。因为读取时会把字节转为字符, 写出时还要把字符转回字节.
  • 程序需要读取一段文本, 或者需要写出一段文本的时候可以使用字符流

96、字符流是否可以拷贝非纯文本的文件

  • 不可以。
  • 因为在读的时候会读2个字节(将字节转换为字符),在转换过程中,可能找不到对应的字符,就会用?代替,写出的时候会将字符转换成字节写出去
  • 如果是?,直接写出,这样写出之后的文件就乱了,看不了了

97、自定义字符数组的拷贝

    FileReader fr = new FileReader("aaa.txt");      FileWriter fw = new FileWriter("bbb.txt");      int len;    char[] arr = new char[1024*8];                  while((len = fr.read(arr)) != -1) {                 fw.write(arr, 0, len);                      }    fr.close();                                     fw.close(); 

98、带缓冲的字符流(BufferedReader、BufferedWriter)

  • BufferedReader的read()方法读取字符时会一次读取若干字符到缓冲区, 然后逐个返回给程序, 降低读取文件的次数, 提高效率
  • BufferedWriter的write()方法写出字符时会先写到缓冲区, 缓冲区写满时才会写到文件, 降低写文件的次数, 提高效率

    BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));  BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));  int ch;             while((ch = br.read()) != -1) {     //read一次,会先将缓冲区读满,从缓冲区中一个一个的返给临时变量ch    bw.write(ch);                   //write一次,是将数据装到字符数组,装满后再一起写出去}br.close();                         bw.close();  
  • readLine():读取一行字符(不包含换行符号)
  • newLine():输出一个跨平台的换行符号”\r\n”

    BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));String line;while((line = br.readLine()) != null) {    bw.write(line);    //bw.write("\r\n");     //只支持windows系统    bw.newLine();           //跨平台的}br.close();bw.close(); 

99、LineNumberReader类

  • LineNumberReader是BufferedReader的子类, 具有相同的功能, 并且可以统计行号

    • 调用getLineNumber()方法可以获取当前行号
    • 调用setLineNumber()方法可以设置当前行号

      LineNumberReader lnr = new LineNumberReader(new FileReader("aaa.txt"));String line;lnr.setLineNumber(100);  //设置行号,行号从101开始while((line = lnr.readLine()) != null) {    System.out.println(lnr.getLineNumber() + ":" + line);//获取行号}lnr.close(); 

100、装饰设计模式

  • Java 的I/O API就是使用装饰设计模式(Decorator)实现的。
  • 大学4年里最喜欢的刘伟老师——设计模式之装饰设计模式

101、使用指定的码表读写字符(InputStreamReader、OutputStreamWriter)

  • FileReader 是使用默认码表读取文件, 如果需要使用指定码表读取, 那么可以使用InputStreamReader(字节流,编码表)
  • FileWriter 是使用默认码表写出文件, 如果需要使用指定码表写出, 那么可以使用OutputStreamWriter(字节流,编码表)
  • InputStreamReader 将字节流转化为字符流
  • OutputStreamWriter 将字符流转化为字节流

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("UTF-8.txt"), "UTF-8")); //高效的用指定的编码表读             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("GBK.txt"), "GBK")); //高效的用指定的编码表写int ch;while((ch = br.read()) != -1) {    bw.write(ch);}br.close();bw.close();

102、字符流实例

  • 获取一个文本上每个字符出现的次数,将结果写在times.txt上

    BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));HashMap<Character, Integer> hm = new HashMap<>();int c;while((c = br.read()) != -1) {    char ch = (char)c;    hm.put(ch, !hm.containsKey(ch)? 1 : hm.get(ch) + 1);}br.close();BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));for (Character key : hm.keySet()) {    bw.write(key + "=" + hm.get(key));}bw.close();
  • 当我们下载一个试用版软件,没有购买正版的时候,每执行一次就会提醒我们还有多少次使用机会,用学过的IO流知识,模拟试用版软件,试用10次机会,执行一次就提示一次您还有几次机会,如果次数到了提示请购买正版

    BufferedReader br = new BufferedReader(new FileReader("config.txt"));String line = br.readLine();int times = Integer.parseInt(line);                 if(times > 0) {    System.out.println("您还有" + times-- + "次机会");    FileWriter fw = new FileWriter("config.txt");    fw.write(times + "");    fw.close();}else {    System.out.println("您的试用次数已到,请购买正版");}br.close();
  • 从键盘输入接收一个文件夹路径,打印出该文件夹下所有的.java文件名

    public static void main(String[] args) {    File dir = getDir();    printJavaFile(dir);}//获取键盘录入的文件夹路径public static File getDir() {    Scanner sc = new Scanner(System.in);                System.out.println("请输入一个文件夹路径");    while(true) {        String line = sc.nextLine();//将键盘录入的文件夹路径存储        File dir = new File(line);  //封装成File对象        if(!dir.exists()) {            System.out.println("您录入的文件夹路径不存在,请重新录入");        }else if(dir.isFile()) {            System.out.println("您录入的是文件路径,请重新录入文件夹路径");        }else {            return dir;        }    }}//获取文件夹路径下的所.java文件public static void printJavaFile(File dir) {    //获取到该文件夹路径下的所有的文件和文件夹,存储在File数组中    File[] subFiles = dir.listFiles();    //遍历数组,对每一个文件或文件夹做判断    for (File subFile : subFiles) {        //如果是文件,并且后缀是.java的,就打印        if(subFile.isFile() && subFile.getName().endsWith(".java")) {            System.out.println(subFile);        //如果是文件夹,就递归调用        }else if (subFile.isDirectory()){            printJavaFile(subFile);        }    }}
原创粉丝点击