缓冲流、转换流、序列化流

一、缓冲流

1.1字节缓冲流:

​ BufferedOutputStream: 字节输出缓冲流, 用来写。
BufferedInputStream: 字节输入缓冲流, 用来读。

​ 缓冲流内部有一个缓冲区数组,所以他的效率是比较高的。其实缓冲流本身并不具备读或者写的功能,它其实是为其他流提供加速。

使用步骤:
    1. 创建缓冲流对象。
    2. 调用方法,读或者写(读写的方法和昨天学的FileOutputStream和FileInputStream一模一样)
    3. 释放资源。
缓冲流的构造方法:

​ BufferedInputStream(InputStream in): 需要传递一个字节输入流
BufferedOutputStream(OutputStream out) 需要传递一个字节输出流

public class Demo02BufferedStream {
    //使用缓冲流,一次读写一个字节的方式复制文件。
    public static void main(String[] args) throws IOException {
        //创建缓冲流输入对象, 用来读取
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa.jpg"));
        //创建缓冲流输出对象, 用来写
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bb.jpg"));
        //开始复制,一次读写一个字节。
        long start = System.currentTimeMillis();

        int i;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start); //142
        //释放资源
        bos.close();
        bis.close();
    }
}
读写数组复制文件
/*
     使用缓冲流一次读写一个数组复制文件

 */
public class Demo03BufferedStream {
    public static void main(String[] args) throws IOException {
        // 创建一个BufferedInputStream,用来读
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa.jpg"));
        // 创建一个BufferedOutputStream, 用来写
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bb.jpg"));

        long start = System.currentTimeMillis();
        //开始复制,一次读写一个数组
        byte[] bArr = new byte[1024 * 8];
        int len;
        while ((len = bis.read(bArr)) != -1) {
            bos.write(bArr, 0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start); //10

        //释放资源
        bos.close();
        bis.close();

    }
}
1.2字符缓冲流:

​ BufferedReader:字符输入缓冲流

​ BufferedWriter:字符输出缓冲流

​ 字符缓冲流同样自己也并不具备读或者写的功能, 他其实是为其他流提供加速。

字符缓冲流的基本使用:
1. 创建流对象。
2. 调用读或者写的方法(读或者写的方法和我们昨天学的FileReader和FileWriter一模一样)
3. 释放资源。
缓冲流的构造方法:

​ BufferedWriter(Writer out): 参数需要传递一个字符输出流(FileWriter)
BufferedReader(Reader in): 参数需要传递一个字节输入流(FileReader)

字符缓冲流中特有的功能

​ 1.BufferedWriter中有一个特有的方法,可以写一个跨平台的换行符:
void newLine(): 实现一个跨平台的换行符

​ 2.bufferedReader里面也有一个特有的方法,可以一次读取一行:
String readLine():一次读取一行数据,并返回读取到的这行, 如果读取结束返回null

readLine不会读取换行符,只会读取换行符之前的内容

public class Demo04BufferedStream {
    public static void main(String[] args) throws IOException {
        method2();
    }

    /*
        使用BufferedReader 从文件中读数据
     */
    public static void method2() throws IOException {
        //创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("file01.txt"));
        //开始读
        char[] bArr = new char[1024];
        int len;
        while ((len = br.read(bArr)) != -1) {
            //处理读取到的数据
            System.out.print(new String(bArr, 0, len));
        }
        //释放资源
        br.close();
    }
    /*
        使用BufferedWriter向文件中写入数据
     */
    public static void method1() throws IOException {
        //创建缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("file01.txt"));
        //调用方法,向文件中写入数据
        bw.write("请问今天你洗头了吗?");
        //释放资源
        bw.close();
    }
}
读取一行案例:
public class Demo05BufferedStream {
    public static void main(String[] args) throws IOException {
        method2();
    }

    /*
        String readLine():一次读取一行数据,并返回读取到的这行。 如果读取结束返回null
     */
    public static void method2() throws IOException {
        //创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("file02.txt"));
        //使用while循环改进下面的代码。
        //定义变量line保存每次读取到的这行数据
        String line;
        //开始循环依次读取一行数据
        while ((line = br.readLine()) != null) {
            /*
                a). 调用readLine读取一行数据
                b). 把读取到的这行数据赋值给line
                c). 判断line是否不等于null,如果不是,那么表示读取到了数据
             */
            System.out.println(line);
        }

        /*
        //读取,一次读取一行数据
        String line = br.readLine();
        System.out.println(line); //  你好

        line = br.readLine();
        System.out.println(line); //   我好

        line = br.readLine();
        System.out.println(line); //
        */
        //释放资源
        br.close();
    }
    /*
        void newLine(): 实现一个跨平台的换行符
     */
    public static void method1() throws IOException {
        //创建一个字符缓冲流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("file02.txt"));
        //调用方法,写数据
        bw.write("你好");
        //调用 void newLine(): 实现一个跨平台的换行符
        bw.newLine();
        bw.write("我好");
        bw.flush();
        bw.close();
    }
}
1.3恢复文本顺序的案例
/*
    请将文本信息恢复顺序

    文本文件的特点:
        1. 每一句话都是 由 序号.内容 组成的。
        2. 序号没有重复。
        3. 每个序号对应一个内容。

    分析:
        1. 定义Map集合,存放每一句话的序号以及对应的内容。
        2. 创建BufferedReader,用来读取。
        3. 开始读取,一次读取一行数据。
        4. 把读取到的内容根据. 切割。
        5. 切割之后就得到了序号以及内容,然后添加到Map集合。
        6. 释放资源。
        7. 定义BufferedWriter,用来写
        8. 使用循环遍历1-Map集合的大小。 然后拿到里面的每一个数字(1-9之间的数字)
        9. 根据这个数字去Map集合中当做key获取value。然后把键以及值进行拼接写入到文件。
        10. 释放资源
 */
public class Demo06BufferedTest {
    public static void main(String[] args) throws IOException {
        //定义Map集合,存放每一句话的序号以及对应的内容。
        Map<Integer, String> map = new HashMap<>();
        //创建BufferedReader,用来读取。
        BufferedReader br = new BufferedReader(new FileReader("出师表.txt"));
        //开始读取,一次读取一行数据。
        String line;
        while ((line = br.readLine()) != null) {
            //把读取到的内容根据. 切割。
            //这个split根据正则表达式切割的。 而这个.是正则表达式的特殊字符,所以要根据.切割需要转义
            //切割之后得到了一个字符串数组。 strArr[0]是 序号,strArr[1] 是内容。
            String[] strArr = line.split("\\.");
            //切割之后就得到了序号以及内容,然后添加到Map集合。
            Integer id = Integer.parseInt(strArr[0]);// 序号
            String content = strArr[1]; //内容
            map.put(id, content);
        }
        br.close();
        //定义BufferedWriter,用来写
        BufferedWriter bw = new BufferedWriter(new FileWriter("outTeacherTable.txt"));
        //使用循环遍历1-Map集合的大小。 然后拿到里面的每一个数字(1-9之间的数字)
        for(int i = 1; i <= map.size(); i++) {//
            //把i当做key然后去Map集合获取value
            String content = map.get(i);
            bw.write(i + "." + content);
            //换行
            bw.newLine();
            //刷新
            bw.flush();
        }
        //释放资源
        bw.close();
    }
}

二、转换流

2.1字符编码

​ 常见字符编码:GBK–1/2个字节、UTF-8–1/2/3/4个字节、Unicode–2个字节、ASCII --1个字节、ISO-8859-1–2个字节(拉丁文)。

2.2InputStreamReader

​ InputStreamReader是转换流, 是字节通向字符的桥梁。

​ InputStreamReader是用来读取的。这个流可以指定编码进行读取。

使用步骤:
    1. 创建转换流
    2. 调用方法,进行读取。
    3. 释放资源。
构造方法:

​ InputStreamReader(InputStream in): 是使用平台默认的编码方法进行读取。
InputStreamReader(InputStream in, String charsetName): 是使用指定的编码方式进行读取。

注意: 其实转换流本身也不具备读或者写的功能,转换流做的工作其实是查码表转码的这样的工作。

public class Demo02InputStreamReader {
    public static void main(String[] args) throws IOException {
		//readGBK();
        readUTF8();
    }


    	//读取UTF-8的文件
    public static void readUTF8() throws IOException {
        //创建转换流对象
        //如果不指定编码,默认采用的是平台编码,就是UTF-8
        //InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file02-utf8.txt"));

        //指定UTF-8
        InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file02-utf8.txt"), "utf8");
        int i;//定义i用来保存读取到的字符
        while((i = isr.read()) != -1) {
            System.out.print((char)i);
        }
        isr.close();
    }

    	//读取GBK的文件
    public static void readGBK() throws IOException {
        //创建InputStreamReader对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file01-gbk.txt"), "gbk");
        //调用方法,进行读取
        int i;//定义i用来保存读取到的字符
        while((i = isr.read()) != -1) {
            System.out.print((char)i);
        }
        isr.close();
    }
}
2.3OutputStreamWriter

​ OutputStreamWriter是转换流, 是字符通向字节的桥梁。
OutputStreamWriter 用来,并且可以指定编码进行写入。

使用步骤
1. 创建缓冲流对象
2. 调用方法,写。
3. 释放资源。
构造方法

​ OutputStreamWriter(OutputStream out) :是使用平台默认的编码。
OutputStreamWriter(OutputStream out, String charsetName): 使用指定的编码 。

public class Demo03OutputStreamWriter {
    public static void main(String[] args) throws IOException {
        method2();
    }
//以UTF-8的格式写
    public static void method2() throws IOException {
        //创建转换流对象
        //使用的是平台默认的utf-8编码
        //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file04-utf8.txt"));
        //指定utf-8编码
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file04-utf8.txt"), "utf-8");
        //调用write方法,写数据
        osw.write("你好");
        //释放资源
        osw.close();
    }
//写GBK格式的数据
    public static void method1() throws IOException {
        //创建转换流对象
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file03-gbk.txt"), "gbk");
        //调用方法,写
        osw.write("你好");
        //调用close方法,释放资源
        osw.close();
    }
}

三、序列化Serializable

3.1序列化流

ObjectOutputStream 是序列化流, 可以将java程序中的对象写入到文件中。

步骤:
1. 创建序列化流对象。
2. 调用方法,写对象。
3. 关流。
构造方法:

​ ObjectOutputStream(OutputStream out): 需要传递一个字节输出流。

写对象的方法:

​ void writeObject(Object obj): 向文件中写对象。

注意: 要写入到文件的对象,一定要实现序列化接口(Serializable 接口)。

public class Demo01ObjectOutputStream {
    public static void main(String[] args) throws IOException {
        //创建一个序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file3-obj.txt"));
        //创建一个Person对象
        Person p = new Person("八神庵", 20);
        //调用writeObject把这个对象写到文件中国你
        oos.writeObject(p);
        //释放资源
        oos.close();
    }
}
3.2反序列化流

ObjectInputStream 叫做反序列化流, 可以将文件中的对象读取到java程序中。

使用步骤
1. 创建ObjectInputStream对象。
2. 调用方法,读取对象
3. 释放资源。
构造方法:

​ ObjectInputStream(InputStream in):传递一个字节输入流。

读取:

​ Object readObject(): 从文件中读取对象。

假如读取的时候,要读取的对象的类已经不存在了,就会报错 ClassNotFoundException

public class Demo02ObjectInputStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1. 创建一个ObjectInputStream对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file3-obj.txt"));
        //2. 调用readObject方法,读取对象
        Object obj = ois.readObject();
        Person p = (Person)obj;
        //3. 打印
        System.out.println(p.getName() + "--" + p.getAge());
        //释放资源
        ois.close();
    }
}
3.3注意事项(版本号、序列号)
  • 被static修饰的成员不能被序列化, 被static修饰的成员属于类不属于对象。
  • 如果希望某个成员变量不会写入到文件,并且也不希望使用static关键字。那么可以使用一个关键字这个关键字叫做transient ,这个transient意思是瞬态,被transient修饰的成员不能被序列化
public class Demo03StaticAndTransiend {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //writeObject();
        readObject();
    }
    //从文件中读对象
    public static void readObject() throws IOException, ClassNotFoundException {
        //创建一个反序列化流对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file4-obj.txt"));
        //调用readObject方法,从文件中读取对象
        Object obj = ois.readObject();
        Person p = (Person)obj;
        System.out.println(p.getName() + "-" + p.getAge());
        //释放资源
        ois.close();
    }
    //向文件中写对象
    public static void writeObject() throws IOException {
        //创建一个序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file4-obj.txt"));
        //创建对象,并且把这个对象写到文件中
        oos.writeObject(new Person("牛郎", 12));
        //调用close方法,事项资源
        oos.close();
    }
}
  • 另外,当JVM反序列化对象时,能找到class文件,但是class文件在序列化对象之后发生了修改,那么反序列化操作也会失败,抛出一个 InvalidClassException 异常。发生这个异常的原因如下:
    • 该类的序列版本号与从流中读取的类描述符的版本号不匹配
    • 该类包含未知数据类型
    • 该类没有可访问的无参数构造方法
  • 解决方案
    • private static final long serialVersionUID = 1L
    • 此时表示定义了一个固定的版本号,不管这个类怎么修改,版本号永远是1
3.4序列化集合

需求:

  1. 将存有多个自定义对象的集合序列化操作,保存到 list.txt 文件中。
  2. 反序列化 list.txt ,并遍历集合,打印对象信息

分析:
1. 创建一个集合对象。
2. 添加学生对象。
3. 创建ObjectOutputStream序列化流,将对象(集合)写入到文件。
4. 释放资源。
5. 创建ObjectInputStream,用来读取。
6. 调用readObject,将文件中的对象读取出来。
7. 释放资源

public class Demo05ObjectStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建一个集合对象。
        ArrayList<Person> list = new ArrayList<>();
        //调用add方法,向集合中添加数据
        list.add(new Person("大幂幂", 18));
        list.add(new Person("小甜甜", 20));
        list.add(new Person("大甜甜", 40));
        //创建ObjectOutputStream序列化流,将对象(集合)写入到文件。
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file6-obj.txt"));
        //调用writeObject写对象
        oos.writeObject(list);
        //释放资源。
        oos.close();
        
        //创建ObjectInputStream,用来读取。
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file6-obj.txt"));
        //调用readObject方法,进行读取
        Object obj = ois.readObject();
        ArrayList<Person> newList = (ArrayList<Person>)obj;
        //遍历
        for (Person person : newList) {
            System.out.println(person.getName() + "--" + person.getAge());
        }
        //释放资源
        ois.close();
    }
}

四、打印流

​ 之前一直写的System.out.println 里面就用到了打印流 System.out就是打印流,这个打印流的目的地是控制台。

​ 打印流:
PrintStream:字节打印流
PrintWriter: 字符打印流。

打印流:

​ 只能写,不能读。

​ PrintStream构造方法:
PrintStream(File file): 传递一个File类型的文件。
PrintStream(OutputStream out): 传递一个字节输出流
PrintStream(String fileName): 传递一个字符串类型的文件路径

​ PrintStream的特有写的方法:
print() 输出但不换行
println() 输出并换行。

setOut() 改变流向

public class Demo01PrintStream {
    public static void main(String[] args) throws FileNotFoundException {
        //创建一个打印流对象
        PrintStream ps = new PrintStream("file7-obj.txt");
        //调用方法,写数据
        //ps.println("你好");
        //ps.println("我好");
        System.setOut(ps);//此时System.out 这个流的目的地就不再是控制台,是ps这个流指向的文件
        System.out.println("哈哈哈");
        //调用方法释放资源
        ps.close();
    }
}

五、其他IO

BIO: 同步阻塞IO
NIO: 同步非阻塞IO。 dubbo, netty。
AIO: 异步非阻塞IO。
/*
    jdk7之后多了一个Files工具类,里面使用的就是NIO

    用到的API
    Files: 工具类,是io操作的核心类。
    Path: 接口,路径。
    Paths: 工具类, 用来获取Path对象

    Paths:
        static Path get(String first, String... more): 使用多个路径片段获取一个Path对象

    Files里面的方法:
        static Path copy(Path source, Path target, CopyOption... options)
        参数source:表示源文件
        参数target:表示目标文件
        参数options:复制方式。 使用 StandardCopyOption.REPLACE_EXISTING 表示如果文件已经存在就覆盖
        static List<String> readAllLines(Path path):读取文件中的所有行,并放入到集合
 */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        method2();
    }

    public static void method2() throws IOException {
        Path path = Paths.get("outTeacherTable.txt");
        List<String> list = Files.readAllLines(path);
        for (String s : list) {
            System.out.println(s);
        }
    }
    public static void method1() throws IOException {
        //获取一个源
        Path source = Paths.get("d:", "aa.jpg");
        //获取一个目的地
        Path target = Paths.get("e:", "aa.jpg");

        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    }
}

aa

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值