File.io讀取文件(六)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//創建數據源
Student st=new Student("楊冪",33);
File fos=new File(destpath);//無論保存什麼格式類型的文件,都是亂碼,這裏
try(//jdk1.7,資源關閉新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道數據
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要實現對象反序列化/序列化必須實現Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修飾
public Student() {
}
public Student(String name, int age) {
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 "Student [name=" + name + ", age=" + age + "]";
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//創建數據源
Student st=new Student("楊冪",33);
File fos=new File(destpath);//無論保存什麼格式類型的文件,都是亂碼,這裏
try(//jdk1.7,資源關閉新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道數據
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要實現對象反序列化/序列化必須實現Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修飾
public Student() {
}
public Student(String name, int age) {
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 "Student [name=" + name + ", age=" + age + "]";
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.