package Jsu.Com;
import java.io.*;
import java.sql.SQLOutput;
public class Class {
public static void main(String[] args) {
Student jj = new Student(10086,"jj");
//Serializable("D://test//jiejie",jj);
//Object o = reSerializable("D://test//jiejie");
//Student jj = (Student) o;
System.out.println(jj.getName());
jj.setAge(100000086);
System.out.println(jj.getAge());
//Object o = reSerializable("D://jjobj");
//Student jj =(Student) o;
//System.out.println(jj.getName());
}
public static Object Serializable(String path,Object obj){
File file = new File(path);
FileOutputStream fos =null;
ObjectOutputStream oos =null;
try{
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
}catch(IOException e){
e.printStackTrace();
}finally {
if(oos!=null){
try{
oos.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(fos!=null){
try{
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return obj;
}
public static Object reSerializable(String path){
File file = new File(path);
Object obj = null;
FileInputStream fis =null;
ObjectInputStream ois =null;
try{
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
obj = ois.readObject();
}catch (IOException|ClassNotFoundException e){
e.printStackTrace();
}finally {
if(ois!=null){
try{
ois.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return obj;
}
}
package Jsu.Com;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable{
private static final long serialVersionUID=10086L;
private int id;
private String name;
private int age;
public Student(int id,String name){
this.id=id;
this.name=name;
}
public Student(){
}
public int getId(){
return id;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return super.toString();
}
}
练习aaaaa
最新推荐文章于 2024-10-03 09:02:12 发布