序列化和反序列化
序列化就是将内存中的对象输出到硬盘文件中进行保存,反序列化就是将硬盘中保存的数据还原成内存中的对象。
- 序列化
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class Test2 {
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("张三");
user.setAge(22);
File file = new File("C:/Users/73981/Desktop/java.txt");
try {
OutputStream outputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(user);
objectOutputStream.flush();
objectOutputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 反序列化
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class Test3 {
public static void main(String[] args) {
File file = new File("C:/Users/73981/Desktop/java.txt");
try {
InputStream inputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user = (User) objectInputStream.readObject();
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getAge());
objectInputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class Test4 {
public static void main(String[] args) {
//使用IO流完成文件复制
File file = new File("/Users/southwind/Desktop/1.jpg");
try {
// InputStream inputStream = new FileInputStream(file);
// byte[] bytes = new byte[100];
// int length = inputStream.read(bytes);
// File file2 = new File("/Users/southwind/Desktop/test2.txt");
// OutputStream outputStream = new FileOutputStream(file2);
// outputStream.write(bytes,0,length);
InputStream inputStream = new FileInputStream(file);
File file2 = new File("/Users/southwind/Desktop/2.jpg");
OutputStream outputStream = new FileOutputStream(file2);
int temp = 0;
while((temp = inputStream.read())!=-1) {
outputStream.write(temp);
}
// Reader reader = new FileReader(file);
// int temp = 0;
// Writer writer = new FileWriter(file2);
// while((temp = reader.read())!=-1) {
// writer.write(temp);
// }
// writer.close();
// reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class Test5 {
public static void main(String[] args) {
File file = new File("/Users/southwind/Desktop/1.jpg");
try {
// InputStream inputStream = new FileInputStream(file);
// BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// File file2 = new File("/Users/southwind/Desktop/2.jpg");
// OutputStream outputStream = new FileOutputStream(file2);
// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
// int temp = 0;
// while((temp = bufferedInputStream.read())!=-1) {
// bufferedOutputStream.write(temp);
// }
// bufferedInputStream.close();
// bufferedOutputStream.close();
// inputStream.close();
// outputStream.close();
InputStream inputStream = new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
File file2 = new File("/Users/southwind/Desktop/2.jpg");
OutputStream outputStream = new FileOutputStream(file2);
Writer writer = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
int temp = 0;
while((temp = bufferedReader.read())!=-1) {
bufferedWriter.write(temp);
}
bufferedWriter.close();
bufferedReader.close();
reader.close();
writer.close();
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}