javaoop中的反序列化
(语言为Java) 我这里有个问题。我尝试过搜索,但没有找到任何东西(可能我的搜索参数不正确,我不知道)。 假设我有一个person.txt文件,它是: 乔;德国;25; joe@hotmail.com 我有一门课,如下所示:javaoop中的反序列化,java,oop,deserialization,Java,Oop,Deserialization,(语言为Java) 我这里有个问题。我尝试过搜索,但没有找到任何东西(可能我的搜索参数不正确,我不知道)。 假设我有一个person.txt文件,它是: 乔;德国;25; joe@hotmail.com 我有一门课,如下所示: public class Person implements Serializable { private String name; private String country; private String age; private S
public class Person implements Serializable {
private String name;
private String country;
private String age;
private String email;
public Person(String name, String country, String age, String email) {
this.name = name;
this.country = country;
this.age = age;
this.email = email;
}
(getters and setters)
}
我想对person.txt文件进行反序列化,以便获得person的实例,但无论我如何努力,我都不确定如何才能做到这一点。我知道在这个过程中的某个地方我必须使用拆分(;),但我在这方面遇到了问题。要反序列化java对象,需要在类中定义一个
无参数构造函数。所以你的人
类看起来像这样
public class Person implements Serializable
{
private String name;
private String country;
private String age;
private String email;
public Person( String name, String country, String age, String email )
{
this.name = name;
this.country = country;
this.age = age;
this.email = email;
}
// no-args constructor
public Person()
{
}
// standard getters and setters goes here
}
除此之外,序列化和反序列化非常简单
public static void main( String[] args ) throws IOException, ClassNotFoundException
{
Person p = new Person( "Test", "Au", "38", "test@gmail.com" );
FileOutputStream f = new FileOutputStream( new File( "Serialize.ser" ) ); // This can be any name . For ex : person.txt
ObjectOutputStream o = new ObjectOutputStream( f );
// Write object to file
o.writeObject( p );
o.flush();
o.close();
f.flush();
f.close();
FileInputStream fi = new FileInputStream( new File( "Serialize.ser" ) );
ObjectInputStream oi = new ObjectInputStream( fi );
// Read objects
Person personInput = ( Person ) oi.readObject();
System.out.println( personInput.toString() );
oi.close();
fi.close();
}
您也可以使用简单的文件处理来提取person类对象数组中的数据,然后根据需要使用它。下面是一个示例代码,可以理解这一点:
public class Person implements Serializable {
private String name;
private String country;
private String age;
private String email;
public Person(String name, String country, String age, String email) {
this.name = name;
this.country = country;
this.age = age;
this.email = email;
}
public person(){}
}
public class ReadLineByLine
{
public static void main(String args[])
{
try
{
FileInputStream fis=new FileInputStream("person.txt");
Scanner sc=new Scanner(fis);
String tempLineData = "";
String[] elements;
ArrayList <Person> People = new ArrayList<Person>();
while(sc.hasNextLine())
{
tempLineData=sc.nextLine();
elements = tempLineData.split(";");
Person p = new Person( (elements[0].trim()), (elements[1].trim()), (elements[2].trim()), (elements[3].trim()) );
People.add(p);
}
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public类Person实现可序列化{
私有字符串名称;
私人国家;
私弦时代;
私人字符串电子邮件;
公众人物(字符串名称、字符串国家/地区、字符串年龄、字符串电子邮件){
this.name=名称;
这个国家=国家;
这个。年龄=年龄;
this.email=电子邮件;
}
公众人物(){}
}
公共类ReadLineByLine
{
公共静态void main(字符串参数[])
{
尝试
{
FileInputStream fis=新的FileInputStream(“person.txt”);
扫描仪sc=新扫描仪(fis);
字符串tempLineData=“”;
字符串[]元素;
ArrayList People=新建ArrayList();
while(sc.hasNextLine())
{
tempLineData=sc.nextLine();
elements=tempLineData.split(“;”);
Person p=新人员((元素[0].trim()),(元素[1].trim()),(元素[2].trim()),(元素[3].trim());
新增(p);
}
sc.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
这是一份大学作业,我看错了。我以为我们应该阅读.lft文件,或者.ser文件,显然不是这样,因为可以使用简单的.txt文件,因此我可以使用您提出的任何解决方案(我在上学期已经做过)以及while循环。非常感谢。