Java IO & Serialization
专为开卷考试准备,内容包括基本的文本文件和二进制文件的读写以及序列化反序列化操作
IO demo
package helloworld;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestIO {
public static void main(String[] args) {
String fileName = "test.txt";
try {
FileWriter fWriter = new FileWriter(fileName);
fWriter.write("你好
");
fWriter.write("Hello World");
fWriter.close();
// 带缓冲的写法,写入大量内容的时候可以更加高效一些
BufferedWriter fWriter2 = new BufferedWriter(new FileWriter("test2.txt")); // 装饰器模式
fWriter2.write("加入buffer
");
fWriter2.write("Hi World");
fWriter2.close();
// 读取文件
BufferedReader fReader = new BufferedReader (new FileReader(fileName));
StringBuffer content = new StringBuffer();
String line;
while ((line = fReader.readLine())!=null) {
content.append(line);
}
System.out.println(content);
// 你好
Hello World
fReader.close();
} catch (IOException e) {
e.printStackTrace();
}
// 二进制文件
try {
// 打开流
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("test.bin"));
dataOutputStream.writeInt(123);
dataOutputStream.writeUTF("方浩 the great");
dataOutputStream.writeDouble(1234.56);
dataOutputStream.close(); // 关闭流
// 读取二进制文件
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("test.bin"));
System.out.println(dataInputStream.readInt());
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readDouble());
dataInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serialization demo
package helloworld;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// 要序列化的的对象需要实现序列化接口
class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String name;
String address;
void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}
public class TestSerialization {
public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "Tim";
employee.address = "Phokka Kuan, Ambehta Peer";
// 序列化
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("test.ser"));
// 把对象存入文件
out.writeObject(employee);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 反序列化
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("test.ser"));
Employee e = (Employee)in.readObject();
in.close();
e.mailCheck();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}