您的位置:首页 > 编程语言 > Java开发

JAVA IO流之缓冲流 序列化流 详解

2018-02-01 22:09 309 查看

缓冲流(高效率流)

BufferedOutputStream 缓冲输出字节流

构造方法: BufferedOutputStream(OutputStream out)

参数 : 字节输出流的父类 (常用类 FileOutputStream)

代码公式:

// 抛异常
FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/001.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("Hello World".getBytes());
bos.close();


BufferedIntputStream 缓冲输入字节流

构造方法: BufferedInputStream(InputStream in)

参数 : 字节输入流的父类 (常用类 FileInputStream)

代码公式:

// 抛异常
FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/001.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bs = new byte[1024];
int len = 0;
while ((len = bis.read(bs)) != -1) {
System.out.println(new String(bs,0,len));
}
bis.close();


缓冲字符流

只能写文本

BufferedWrite

构造方法:参数:BufferedWriter(Writer out)

可传入:FileWriter OutputStreamWriter

代码公式:

FileWriter fw = new FileWriter("/Users/james/Desktop/level/001.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hello James");

4000
// 特有方法 换行 无关平台性的 所有平台通用!
bw.newLine();
// 每次写入 要刷新
bw.flush();
bw.close();


BufferedReader

BufferedReader(Reader in)

上同 类似

代码公式:

FileReader fw = new FileReader("/Users/james/Desktop/level/001.txt");
BufferedReader bw = new BufferedReader(fw);
// readLine() 按行读取  但是不能把换行读出
String string ="";
while ((string = bw.readLine()) != null) {
System.out.println(string);
}
bw.close();


Properties 集合

表示了一个持久的属性集 是集合中唯一一个能和IO流配合的类

父类 Hashtable 读取和写入参数的时候 字符/字节都可以

代码示例:

public class Demo06 {
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("name", "james");
properties.setProperty("sex", "男");
properties.setProperty("addres", "china");
// 后缀名给什么都行 但是一般写法 使用.properties 当做文件的后缀名
//来标识该文件可以使用properties 类读取
FileWriter fw = new FileWriter("/Users/james/Desktop/level/001.properties");
// 利用 properties 类中的方法写入
// 参数二 相当于写入文件的注释 一般什么都不写
// 在  properties 中 可以使用 # 来写注释
properties.store(fw, "james");
fw.close();
}
/*
#\u4E01\u9E4F
#Thu Feb 01 14:44:24 CST 2018
addres=china
name=james
sex=男
*/

/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void fun2() throws FileNotFoundException, IOException {
Properties properties = new Properties();
// 读取文件写入properties 中
properties.load(reader);
Set<String> set = properties.stringPropertyNames();
for (String key : set) {
String value = properties.getProperty(key);
System.out.println(key + "=" + value);
}
reader.close();
}

public static void fun1() {
Properties properties = new Properties();
// 注意:该集合最好使用的是 String 字符串类型
// 继承父类 的PUT方法
properties.put("name", "james");
// 本类的调用方法
properties.setProperty("sex", "man");
System.out.println(properties);

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


序列化流 与 反序列化流

序列化 把对象写进文件中 ObjectOutputStream

反序列化 从文件中把对象读出来 ObjectInputStream

注意点

静态的成员变量是不能进行持久化(序列化)的

序列化 序列的是对象 静态的成员变量时输入类的

transient

不想写成静态 也不想序列化 就使用 transient 瞬态关键字

作用: 可以阻止成员变量序列化

序列化通用公式:

public static void writeObject() throws FileNotFoundException, IOException {
// 注意 1. 写对象 都使用字节流来操作
//2. 如果要对 对象 进行实例化 必须要实现 Serializable 接口
// Serializable 该接口属于 标记型接口
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/person.txt");
// 创建对象输出流(序列化流)
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("james", 17));
oos.close();
}


反序列化公式:

public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
// 读取序列化文件(反序列化)
// 在进行反序列化(读取)的时候 需要依赖编译文件 .class文件 来进行读取的
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/level/person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// 读文件
// 这里会报一个异常 ClassNotFoundException
Object object = ois.readObject();
System.out.println(object);
ois.close();
}


ClassNotFoundException

类找不到异常 当访问的类发生变化时候会报出

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: