Java中的缓冲流、Properties集合、序列化流与反序列化流基础解析

本文详细介绍了Java中的IO流概念及应用,包括字节流、字符流、缓冲流等的使用方法,并通过具体示例展示了如何利用缓冲流提高文件读写的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

缓冲流

    缓冲流(高效率的流)
    BufferedOutputstream 缓冲输出字节流
    构造方法:
        BufferedOutputstream(Outputstream out)
        参数: 字节输出流的弗雷 FileOutputStream
        想对哪个流高效 就把这个流 传进去

    BufferedInputStream 缓冲输入字节流

缓冲字节流

    public static void main(String[] args) throws IOException {
        //fun1();
        // 缓冲字节流读取
        FileInputStream fis = new FileInputStream("/Users/s/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));
        }
        bis.close();
    }

    /**
     * @throws FileNotFoundException
     * @throws IOException
     */
     // 缓冲字节流写出
    public static void fun1() throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/ppp.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("HelloWorld".getBytes());
        bos.close();
    }
}

测试高效流复制文件的快慢

使用模板设计模式
    abstract class TestTime{
        // 源文件路径
        public String src = "/Users/s/Desktop/Test/d.png";
        // 目的文件路径
        public String dest = "/Users/s/Desktop/Test/d1.png";
        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 MyCopy1 extends TestTime{
        // 字节流 数组形式 复制文件
        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 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();
    }

}

缓冲字符流

    BufferedWriter
    构造方法:
    参数: Writer
    可以传: FileWriter OutputstreamWriter
    特有方法:
        newLine()  无关平台型(Mac/Windows)
    public class Demo {
    public static void main(String[] args) throws IOException {
        //fun1();
        // 读取
        FileReader fReader = new FileReader("/Users/s/Desktop/Test/ppp.txt");
        BufferedReader br = new BufferedReader(fReader);
        // 按行来读 
        // 按行读取 是不能把换行读出来的
        // 要跟原文本一样 需要字节加上换行来打印
        String string = "";
        while ((string = br.readLine()) != null) {
            System.out.println(string);
        }

        br.close();
    }


    public static void fun1() throws IOException {
        FileWriter fWriter = new FileWriter("/Users/s/Desktop/Test/ppp.txt");
        BufferedWriter bw = new BufferedWriter(fWriter);
        bw.write("扶大厦之将倾");
        bw.newLine();
        bw.flush();
        bw.write("挽狂澜之既倒");
        bw.newLine();
        bw.flush();
        bw.close();
    }
}

字符缓冲流复制文件

    public class Demo04 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("/Users/s/Desktop/Test/ppp.txt");
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter("/Users/s/Desktop/Test/ppp1.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        // 边读 边写
        String string = "";
        while ((string = br.readLine()) != null) {
            bw.write(string);
            // 读的时候 读不出来换行
            // 需要自己加 
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();
    }
}

关于 流 的一个小总结

    1.明确要做什么操作
    读数据源
    InputStream / Reader
    写到数据目的地
    Outputstream / Writer

    2.明确要操作的是什么内容
    文本 音频 图片 等等...
    要使用字节流(全能流) FileInputStream FileOutputStream
    文本(按什么编码格式读写)
    使用字符流 FileWriter FileReader

    3.明确流要在什么设备上使用
    文本
    网络 通过刘 进行数据交互 --- 字节流
    4. 是否需要提高效率
    Buffered 缓冲流

Properties集合

    Properties集合 是双列集合
    Properties的父类是 Hashtable
    作用: Properties是集合中 唯一一个能和IO流配合的类

    读取和写入时
    参数可以是字符流
    参数也可以是字节流
    public class Demo {
    public static void main(String[] args) throws IOException {
        //fun1();
        //fun2();
        //fun3();

    }

    /**
     * @throws IOException
     */
    public static void fun3() throws IOException {
        Properties properties = new Properties();
        properties.setProperty("a", "hh");
        properties.setProperty("b", "zz1");
        properties.setProperty("你好", "再见");
        // 后缀名 给什么都可以
        // 一般写法 使用 properties 当做文件的后缀名
        // 来标识该文件可以使用 properties类 读取
        FileWriter fWriter = new FileWriter("/Users/s/Desktop/Test/b.properties");
        // 利用Properties类中的方法 写入
        // 参数二: 相当于写入文件的注释 一般什么都不写
        // 在Properties文件中 可以用井号(#)来写注释
        properties.store(fWriter, "");
        fWriter.close();
    }

    /**
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void fun2() throws FileNotFoundException, IOException {
        // 读取
        Properties p1 = new Properties();
        FileReader fr = new FileReader("/Users/s/Desktop/Test/b.properties");
        p1.load(fr);
        System.out.println(p1);
        fr.close();
    }

    /**
     * 
     */
    public static void fun1() {
        Properties properties = new Properties();
        // 注意: 该集合最好使用的是 key和value 都是字符串
        properties.setProperty("gender", "女");

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

序列化流与反序列化流

    序列化: 把文件写入文件中(根据我们现在的学习进度)
    反序列化: 从文件中 把对象 读取出来

    对象流
    ObjectInputStream 反序列化流
    ObjectOutputstream 序列化流

    注意: 静态成员变量 是不能进行序列化的
    序列化 序列是对象 静态成员变量是属于类

    序列化相当于是 把 对象进行持久化

    先创建一个Person类
    public class Person implements Serializable{
    /**
     * 序列化使用的序列号
     * 只要写了这个号 在编译时 系统就不会重新计算 序列号
     */
    private static final long serialVersionUID = 1L;
    // 不想写成静态的 也不想序列化 
    // transient 关键字 瞬态关键字
    // 作用: 可以阻止成员变量序列化
    private transient String name;
    private int age;
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}



    public class Demo {
        public static void main(String[] args) throws IOException, ClassNotFoundException  {
            //writeObject();
            readObject();
        public static void writeObject() throws FileNotFoundException, IOException {
            // 注意: 如果要对对象进行实例化
            //       必须要实现Serializable(序列化接口)
            //        Serializable该接口是 标记型接口
            // 写对象 都是用 字节流去操作
            FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/Person.txt");
            // 对象输出流(序列化流)
            ObjectOutputstream oos = new ObjectOutputstream(fos);
            // 使用写对象的方法
            oos.writeObject(new Person("s", 21);
            oos.close;
        }

        public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
            // 读取序列化文件(反序列化)
            // 在进行反序列化(读取)的时候 需要依赖你的编译文件 .class 文件
            // 来进行读取
            FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/Person.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 读文件
            Object object = ois.readObject();
            System.out.println(object);
            ois.close();
        }
    }  


                                        Day.26

http://blog.youkuaiyun.com/ssssssue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值