IO流2

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IODemo {
    private static void copyFile(String srcPath ,String destPath) throws IOException {

        FileInputStream fileInputStream = new FileInputStream(srcPath);
        FileOutputStream fileOutputStream = new FileOutputStream(destPath);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = fileInputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer,0,len);
        }
        fileInputStream.close();
        fileOutputStream.close();


    }
    public static void main(String[] args) throws IOException {

        copyFile("e:/1/123.jpg","e:/1/1234.jpg");
    }
}

在操作上述方法时,存在一个致命问题,在某些情况下,可能close方法无法被调用,当方法执行时,一旦触发了IOException异常,此时方法就会立刻被终止,从而导致下面的close方法无法被调用。

改进后:

private static void copyFile2(String srcPath ,String destPath)  {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
           fileInputStream = new FileInputStream(srcPath);
            fileOutputStream = new FileOutputStream(destPath);
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer,0,len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileInputStream!=null) {
                    fileInputStream.close();
                }

                if (fileOutputStream!=null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

这时,虽然问题已经解决,但由于处理各种异常,显得代码不直观,可以采用下面这种代码:

private static void copyFile3()  {
        //try语句会在代码执行完毕后自动调用close方法,前提是这个类必须实现Closable接口
    try(FileInputStream fileInputStream = new FileInputStream("e:/1/123.jpg");
    FileOutputStream fileOutputStream = new FileOutputStream("e:/1/1234.jpg")) {
       byte[] buffer = new byte[1024];
       int len = -1;
       while ((len = fileInputStream.read(buffer)) != -1) {
           fileOutputStream.write(buffer,0,len);
       }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    }

利用try语句自动调用close。

BufferedInputStream,BufferdeOutputStream

内置了缓存区

  private static void copyFile() throws IOException {
        //先得创建FileInputStream和FileOutputStream
        FileInputStream fileInputStream = new FileInputStream("e:/1/123.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("e:/1/1234.jpg");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer,0,len);
        }
        //此处设计四个流对象
        //调用这组close,就会自动把内部包含的FileInputStream,FileOutputStream关闭
        //也没有先后关闭顺序
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

此时上述的代码也会遇到之前无法调用close的问题,解决办法也一样。

   private static void copyFile2() {
     try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("e:/1/123.jpg");
     BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("e:/1/1234.jpg"))) {
         int len = -1;
         byte[] buffer = new byte[1024];
         while ((len = bufferedInputStream.read(buffer))!=-1) {
             bufferedOutputStream.write(buffer,0,len);
         }
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
    }

在这里插入图片描述
在这里插入图片描述
刚开始读,当文件剩余内容比较多,一次read就会把缓冲区填满,此时len长度就是缓冲区长度,当文件剩余内容不足1024时,就会把剩余内容都读出来,当文件读完了,再次调用read就会返回-1。
每次的read,缓存区的内容就会变一次,write就会把当前缓存区内容写到文件里。

接下来,我们来感受一下,带缓存区和不带缓存区的差距。

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IODemo3 {
    public static void main(String[] args) {
        testNoBuffer();
        //testBuffer();
    }

    private static void testBuffer() {
        long beg = System.currentTimeMillis();
        try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("d:/1024.jpg"))) {
            int ch = -1;
            while ((ch = bufferedInputStream.read())!=-1) {

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(" buffer:"+(end-beg)+"ms");


    }

    private static void testNoBuffer() {
        long beg = System.currentTimeMillis();
        try(FileInputStream fileInputStream = new FileInputStream("d:/1024.jpg")) {
            int ch = -1;
            while ((ch = fileInputStream.read())!=-1) {

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("no buffer:"+(end-beg)+"ms");

    }

}

在这里插入图片描述
在这里插入图片描述

我们可以发现两者的差别还是很大的。

字符流对象

和字节流非常相似,只是字节流读写操作以byte为单位。缓存区就是一个byte【】,字符流读写操作以char为单位,缓存区就是一个char【】。

import java.io.*;

public class IODemo4 {
    public static void main(String[] args) {
        copyFile3();

    }
    private static void copyFile() {
        //处理文本文件
        try(FileReader fileReader = new FileReader("d:/890.txt");
            FileWriter fileWriter = new FileWriter("d:/8901.txt")) {
           char[] buffer = new char[1024];
           int len = -1;
           while ((len = fileReader.read(buffer))!=-1) {
               fileWriter.write(buffer,0,len);
           }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void copyFile2() {
        //处理文本文件
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader("d:/890.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:/8901.txt"))) {
         char[] buffer = new char[1024];
         int len = -1;
         while ((len = bufferedReader.read(buffer))!=-1) {
             bufferedWriter.write(buffer,0,len);
         }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void copyFile3() {
        //带缓存区的字符流有一种特殊的用法,按行读取
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader("d:/890.txt"));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:/8901.txt"))) {
            String line = "";
            //readLine表示读取一行,读到换行符为止
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println("line:"+line);
                bufferedWriter.write(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
readLine读到一行数据,会自动把最末尾的换行符去掉。

序列化和反序列化

序列化:把一个结构化数据变成一个二进制的bit流(可以把这个bit流保存到文件,或者通过网络传输)
反序列化:把二进制bit流还原回原来的对象

反序列化和序列化的最大目的就是为了让对象能够通过网络传输,或者能够在文件中保存。

Java中自带的序列化方式:
ObjectInputStream:负责反序列化
ObjectOutputStream:负责序列化

import java.io.*;

class Student implements Serializable {
    public String name;
    public  int age;
    public int score;
}
public class IODemo5 {
    public static void main(String[] args) throws IOException {
        Student s = new Student();
        s.name = "张三";
        s.age = 20;
        s.score = 100;
        serializeStudent(s);
    }

    private static void serializeStudent(Student s) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.txt"));
        objectOutputStream.writeObject(s);
        objectOutputStream.close();
    }
}

在这里插入图片描述

import java.io.*;

class Student implements Serializable {
    public String name;
    public  int age;
    public int score;
}
public class IODemo5 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
      /*  Student s = new Student();
        s.name = "张三";
        s.age = 20;
        s.score = 100;
        serializeStudent(s);*/
      Student s = deserializeStudent();
        System.out.println(s.age);
        System.out.println(s.name);
        System.out.println(s.score);
    }

    private static void serializeStudent(Student s) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.txt"));
        objectOutputStream.writeObject(s);
        objectOutputStream.close();
    }
    private static Student deserializeStudent() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/student.txt"));
        Student s =(Student) objectInputStream.readObject();
        return s;
    }
}

在这里插入图片描述

对于反序列化有可能会遇到某个字段不太一样,编译器会有专门的解决方法,根据类来生成uid的值。如果反序列化程序读取uid和自己类的uid不一样,就不在反序列了。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lhj_loveFang_1105

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值