java 缓冲流 对象流

缓冲流(高效率的流)

1.缓冲流字节流
  BufferedOutputStream  缓冲输出字节流
  构造方法:
  BufferedOutputStream(OutputStream out) 
  参数:字节输出流的父类 FileOutputStream
  想对哪个流高效 就把谁传进去
  BufferedInputStream    缓冲输入字节流

  //缓冲字节流写入
  FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/ppt.txt");
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  bos.write("helloWoerld".getBytes());
  bos.close();
  //缓冲字节流读取
  FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/ppt.txt");
  BufferedInputStream bis = new BufferedInputStream(fis);
  byte[] b = new byte[1024];
  int len = 0;
  while ((len = fis.read(b, 0, len)) != -1) {
        System.out.println(new String(b, 0, len));
        }
        bis.close();    

缓冲字符流

2. BufferedWrite
  构造方法:
  参数:Write(父类)
  可传 FileWrite OutputStreamWrite
  特有方法:
  newLine() 无关平台性   mac \n
                       windows  /r/n
  BufferedReader
  // 文件复制(按行读 按行写)
  FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/ppt1.txt");
        FileReader fr = new FileReader("/Users/lanou/Desktop/Test/ppt.txt");
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        String string = "";
        while ((string = br.readLine()) != null) {
            bw.write(string);
            // 读的时候 读不出来换行
            // 需要自己加
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();

缓冲流高效的体现

3.这里写图片描述

Properties集合

4.Properties集合是双列集合 父类:HashTable
作用: Properties是集合中唯一一个能和 IO流配合的类
读取和写入时参数 可以是字符流 也可以是字节流
//写一个Properties集合
 Properties properties = new Properties();
 //该集合 最好使用 key和value都是字符串
 properties.set.Property("gender", "男");
 // 遍历集合
 // 取出所有key
 Set<String> set = properties.stringPropertyNames();
 for(String key : set){
     String value = properties.getProperty(key);
     System.out.println(key + "==" + value);
     }
//读取
Properties properties = new Properties();
FileReader fr = new FileReader("/Users/lanou/Desktop/Test/q.txt");
properties.load(fr);
System.out.println(properties);
fr.close(); 
//写入
//写入
    Properties properties = new Properties();
    properties.setProperty("a", "haha");
    properties.setProperty("丁鹏", "哈哈");
    properties.setProperty("b", "haha");
    //后缀名 给什么都
    // 但是一般写法 使用.properties 当做文件的后缀名
    // 用来标示该文件可以使用Properties类读取
    FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/xzb.properties");
    // 利用Properties类中的方法写入
    // 参数二:相当于文件的注释 一般什么都不写
    // 在properties文件中 可以使用井号(#)来写注释
    properties.store(fw, "");
    fw.close(); 

对象流(序列化与反序列化)

5.序列化流与反序列化流
  序列化 把对象写进文件中
  反序列化 从文件中把对象读取出来

  对象流
  ObjectInputStream
  ObjectOutputStream

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

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

  //序列化
  public static void writeObject() throws FileNotFoundException, IOException {
        // 注意:如果要对对象进行实例化 必须要实现Serializable(序列化接口) 该接口是标记型接口
        // 注意 写对象都使用字节流去操作
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/person.txt");
        //创建对象输出流(序列化流)
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //使用写对象的方法
        oos.writeObject(new Person("dp", 15));
        oos.close();
    }
//反序列化
public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
        // 读取序列化文件(反序列化)
        // 在进行反序列化(读取)的时候 需要依赖你的便以文件.class文件 来进行读取的
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/person.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // 读文件
        Object object = ois.readObject();
        System.out.println(object);
        ois.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值