Java IO流之使用缓冲流读写及序列化

缓冲流

使用缓冲流的好处是,能够更高效的读写信息,
原理是将数据先缓冲起来,然后一起写入或者读取出来。

缓冲输出字符流

BufferedWriter
    // 写入时使用 .newLine(); 达到换行目的 此方法可以跨平台实现

    public static void write() throws IOException {
        FileWriter fw = new FileWriter("要写入的文件位置");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("明月几时有 把酒问青天");
        bw.newLine();
        bw.flush();
        bw.write("明月几时有 把酒问青天");
        bw.newLine();
        bw.flush();
        bw.close();
    }

缓冲输入字符流

BufferedReader
    public static void read() throws FileNotFoundException, IOException {
        // 读取
        FileReader fr = new FileReader("读取的文件路径");
        BufferedReader br = new BufferedReader(fr);
        // 无法读取换行 换行需要手动加上换行打印
        String string = "";
        while ((string = br.readLine()) != null) {
            System.out.println(string);
        }
        br.close();
    }

        缓冲字符流 复制文件


    public static void main(String[] args) throws IOException {
        // 读取
        FileReader fr = new FileReader("输入的文件路径");
        BufferedReader br = new BufferedReader(fr);
        // 写入
        FileWriter fw = new FileWriter("输出的文件路径");
        BufferedWriter bw = new BufferedWriter(fw);
        // 边读边写 复制
        String string = "";
        while ((string = br.readLine()) != null) {
            bw.write(string);
            // 读的时候 无法读出换行 需手动添加
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();
    }

缓冲输出字节流

BufferedOutputStream
    public static void fun() throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream("/Users/C/Desktop/Test/ppp.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("helloWorld".getBytes());
        bos.close();
    }

缓冲输入字节流

BufferedInputStream
    public static void fun() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("/Users/C/Desktop/Test/ppp.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
    }

测试缓冲流能高效多少

    创建抽象类来计算时间

abstract class TestTime{
    public String src = "要复制的文件路径";
    public String dest = "复制玩存放的文件路径";
    public void printTime() throws IOException {
        long start = System.currentTimeMillis();
        // 调用
        copyFile();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
    // 子类要抛异常 父类必须有这异常
    public abstract void copyFile() throws IOException;
}
    "分别测试: "

    "字节流"  单字节形式  复制文件
              数组形式    复制文件  
    "缓冲流"  单字节形式   复制文件
              数组形式    复制文件
    使用字节流 以字节形式 复制文件

class ByteCopy1 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int len = 0;
        while ((len = fis.read()) != -1) {
            fos.write(len);
        }
        fis.close();
        fos.close();
    }

}

    使用字节流 以数组形式 复制文件

class ByteCopy2 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int len = 0;
        byte[] b = new byte[1024];
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        fis.close();
        fos.close();
    }

}
    使用缓冲流 以字节形式 复制文件

class Mycopy extends TestTime{

    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(dest);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        while ((len = bis.read()) != -1) {
            bos.write(len);
        }
        bis.close();
        bos.close();

    }

}


    使用缓冲流 以数组形式 复制文件

class Mycopy2 extends TestTime{

    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(dest);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        byte[] b = new byte[1024];
        while ((len = bis.read(b)) != -1) {
            bos.write(b,0,len);
        }
        bis.close();
        bos.close();

    }

}
测试后 发现缓冲流能大幅提升效率

序列化流 与 反序列化流

序列化

把对象写进文件中
ObjectOutputStream 序列化流
    public static void writeObject() throws FileNotFoundException, IOException {
        // 注意: 如果要对对象进行实例化
        //      必须要实现Serializable(序列化接口)
        //      Serializable 该接口是 标记性接口
        // 写对象 使用字节流操作
        FileOutputStream fos = new FileOutputStream("序列化的文件路径");
        // 序列化流
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        // 写对象的方法
        oos.writeObject(new Person("zhangsan", 16));
        oos.close();
    }

反序列化

从文件中把对象读取出来
ObjectInputStream 反序列化流
    public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
        // 读取序列化文件(反序列化)
        // 在进行反序列化(读取)的时候 需要依赖编译文件 .class文件 来进行读取的
        FileInputStream fis = new FileInputStream("读取的序列化文件路径");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // 读文件
        Object obj = ois.readObject();
        System.out.println(obj);
        ois.close();
    }

Properties 集合 (双列集合) 父类Hashtable

作用: Properties是集合中唯一一个能和IO流配合的类
读取和写入时参数
  可以使字符流
  也可以字节流
    public static void fun() {
        Properties properties = new Properties();
        properties.put("name",1);
        properties.setProperty("gender", "nv");

        // 遍历集合
        // 取出所有key
        Set<String> set = properties.stringPropertyNames();
        for (String key : set) {
            String value = properties.getProperty(key);
            System.out.println(key + "==" + value);
        }
    }

    public static void fun() throws FileNotFoundException, IOException {
        // 读取
        Properties properties = new Properties();
        FileReader fr = new FileReader("/Users/Desktop/Test/xieru.txt");
        properties.load(fr);
        System.out.println(properties);
        fr.close();
    }

    public static void fun() throws IOException {
        // 写入
        Properties properties = new Properties();
        properties.setProperty("a", "haha");
        properties.setProperty("b", "666");
        properties.setProperty("c", "5415");
        // 一般使用 .properties 作为文件的后缀名
        // 来标识该文件可以使用Properties 类读取
        FileWriter fw = new FileWriter("/Users/Desktop/Test/fun.properties");
        // 利用properties 类中的方法写入
        // 在properties文件中 可以使用井号(#)来写注释
        properties.store(fw, "");
        fw.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值