Java中缓冲流、特殊集合Porperties、序列化和反序列化

缓冲流(高效流):


缓冲字节流:
BufferedOutputStream:缓冲输出字节流
父类:FileOutputStream
构造方法:BufferedOutputStream(OutputStream out)

BufferedInputStream:缓冲输入字节流
父类:FileInputStream
构造方法:BufferedInputStream(InputStream in)
缓冲流复制文件效率比较:
/*
 *计时抽象类
 */
abstract class TestTime {
    public String src = "/Users/lanou/Desktop/Test/dp.png";
    public String dest = "/Users/lanou/Desktop/Test/dp1.png";
    public void printTime() throws IOException {
        long start = System.currentTimeMillis();
        copFile();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
    public abstract void copFile() throws IOException;
}
/*
 * 无缓冲流字节形式复制文件
 * 时间:14105毫秒
 */
class MyCopy1 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStram(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int len = 0;
        while (len = fis.read()) != -1) {
            fos.writer(len);
        }
        fis.close();
        fos.close();
    }
}
/*
 * 有缓冲流字节形式复制文件
 * 时间:254毫秒
 */
class MyCopy2 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStram(src);
        BufferedInputStram bis = new BufferedInputStram(fis);
        FileOutputStream fos = new FileOutputStream(dest);
        BufferedOutputStram bos = new BufferedOutputStram(fos);
        int len = 0;
        while (len = bis.read()) != -1) {
            bos.writer(len);
        }
        bis.close();
        bos.close();
    }
}
/*
 * 无缓冲流数组形式复制文件
 * 时间:27毫秒
 */
class MyCopy3 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStram(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int len = 0;
        byte[] b = new byte[1024];
        while (len = fis.read(b)) != -1) {
            fos.writer(b, 0, len);
        }
        fis.close();
        fos.close();
    }
}
/*
 * 有缓冲流数组形式复制文件
 * 时间:12毫秒
 */
class MyCopy4 extends TestTime {
    @Override
    public void copyFile() throws IOException {
        FileInputStream fis = new FileInputStram(src);
        BufferedInputStram bis = new BufferedInputStram(fis);
        FileOutputStream fos = new FileOutputStream(dest);
        BufferedOutputStram bos = new BufferedOutputStram(fos);
        int len = 0;
        byte[] b = new byte[1024];
        while (len = bis.read(b)) != -1) {
            bos.writer(b, 0, len);
        }
        bis.close();
        bos.close();
    }
}
缓冲字符流:
BufferedWriter:缓冲输出字符流
父类:Writer
构造方法:BufferedWriter(Writer out) 
特有方法:newLine() 
          相当于换行符,Mac、Windows都可以使用,无关平台性

BufferedReader:缓冲输入字符流
父类:Reader
构造方法:BufferedReader(Reader in)
特有方法:readLine()
          具有换行作用,但是读取源文件时,无法将换行读取出来,如需实现换行,需自行添加换行
案例:
/*
 * 文件复制
 */
public static void fun() throws IOException {
    FileReader fr = new FileReader("/Users/lanou/Desktop/Test/ppp.txt");
    BufferedReader br = new BufferedReader(fr);
    FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/ppp1.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    String string = " ";
    while((string = br.readLine()) != null) {
        bw.write(string);
        bw.flush();
    }
    br.close();
    bw.close();
}
字节流、字符流、转换流、缓冲流的总结:
1.明确是读取源文件,还是写到数据目的;
2.对文本、音频、图片等进行操作使用字节流,对文本操作使用字符流
3.确认是否要对读写进行效率提升

Properties集合:

是一个双列集合,它的父类是Hashtable,它是没有泛型的
作用:可以和IO流配合使用
注意:该集合key和value最好都使用字符串
/*
 * 读取
 */
public static void read() throws FileNotFoundException, IOException {
    Properties properties = new Properties();
    FileReader fr = new FileReader("/Users/lanou/Desktop/Test/xzb.properties");
    properties.load(fr);
    System.out.println(properties);
    fr.close();
}
/*
 * 写入
 */
public static void write() throws IOException {
    Properties properties = new Properties();
    properties.setProperty("a", "haha");
    properties.setPorperty("张三", "哈哈哈");
    properties.setPorperty("b", "haha");
    FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/xzb.properties");
    properties.store(fw, "");
    fw.close();
}

序列化流与反序列化流:

ObjectOutputStream:序列化,把对象写进文件中
ObjectInputStraem:反序列化,把对象从文件中读取出来

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

注意:静态变量是不能进行实例化的
      在对对象进行实例化时,要实现Serializable接口,该接口时标记性接口
      进行反序列化时,要依赖.class文件进行读取
案例:
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 "姓名:" + name + ", 年龄:" + age;
    }
}
/*
 * 序列化
 */
public static void WriteObject() throws FileNotFoundException, IOException {
    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 {
    FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/person.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object obj = ois.readObject();
    System.out.println(obj);
    ois.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值