1
我有一個類的UserData序列化對象,StreamCorruptedException而反序列化對象
import java.io.*;
public final class UserData implements Serializable
{
private String name,username,password;
private boolean male;
private int age;
public void setName(String name) { this.name = new String(name); }
public void setUsername(String username) {this.username = new String(username); }
public void setPassword(String password) {this.password = new String(password); }
public void setAge(int age) { this.age = age; }
public void setGender(boolean gender) { this.male = gender; }
public String getName() { return new String(this.name); }
public String getUsername() { return new String(this.username); }
public String getPassword() { return new String(this.password); }
public int getAge() { return this.age; }
public boolean getGender() { return male; }
public static void writeUserDataToFile(UserData data,String fileName)
{
try
{
if(data == null)
{
return;
}
ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(new File(fileName),true));
objOutput.writeObject(data);
objOutput.flush();
objOutput.close();
}
catch(FileNotFoundException ex)
{
System.out.println("Error : Cannot Save Data , The given filename \""+fileName+"\" is not valid.");
return;
}
catch(NotSerializableException ex)
{
System.out.println("Error : It has been found that some data is not Serializable!");
return;
}
catch(IOException ex)
{
System.out.println("Error : IOException has been encountered,");
return;
}
catch(SecurityException S)
{
System.out.println("Error : Security Exception has been Encountered.");
return;
}
catch(Exception e)
{
System.out.println("Error : Unknown Exception thrown!");
return;
}
System.out.println("Sucess : Data written to \""+fileName+"\".");
}
public static UserData checkCredentials(String fileName,String userName,String passWord)
{
try
{
ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(new File(fileName)));
UserData data;
while((data = (UserData)objInput.readObject())!=null)
{
if(userName.equals(data.getUsername()) && passWord.equals(data.getPassword()))
return data;
}
objInput.close();
}
catch(Exception ex)
{
System.out.println("Error : Unknown Exception Caught while deserializing object.");
ex.printStackTrace();
}
return null;
}
}
爲了測試這個類,我寫了另一個類,
public class TestUserData
{
public static void main(String args[])
{
UserData data = new UserData();
System.out.println("Serializing objects .... ");
for(int i=0;i<5;i++)
{
data.setName("John");
data.setAge(10+i);
data.setGender((i%2==0)?true:false);
data.setUsername("John"+i);
data.setPassword(i+"John");
UserData.writeUserDataToFile(data,"testSerial.dat");
}
System.out.println("de-Serializing objects .... ");
for(int i=0;i<5;i++)
{
data = UserData.checkCredentials("testSerial.dat","John"+i,i+"John");
if(data!=null)
{
System.out.println("Name : "+data.getName());
System.out.println("Age : "+data.getAge());
System.out.println("Gender : "+(data.getGender() ? "Male" : "Female"));
}
else
{
System.out.println("Data for Object "+i+" not de-serialized.");
}
}
}
}
輸出我得到
Serializing objects ....
Sucess : Data written to "testSerial.dat".
Sucess : Data written to "testSerial.dat".
Sucess : Data written to "testSerial.dat".
Sucess : Data written to "testSerial.dat".
Sucess : Data written to "testSerial.dat".
de-Serializing objects ....
Name : John
Age : 10
Gender : Male
Error : Unknown Exception Caught while deserializing object.
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at UserData.checkCredentials(UserData.java:67)
at TestUserData.main(TestUserData.java:19)
Data for Object 1 not de-serialized.
Error : Unknown Exception Caught while deserializing object.
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at UserData.checkCredentials(UserData.java:67)
at TestUserData.main(TestUserData.java:19)
Data for Object 2 not de-serialized.
Error : Unknown Exception Caught while deserializing object.
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at UserData.checkCredentials(UserData.java:67)
at TestUserData.main(TestUserData.java:19)
Data for Object 3 not de-serialized.
Error : Unknown Exception Caught while deserializing object.
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at UserData.checkCredentials(UserData.java:67)
at TestUserData.main(TestUserData.java:19)
Data for Object 4 not de-serialized.
在TestUserData類中,我是seri對5個UserData對象進行校準,序列化發生的很好,但在反序列化部分,它不能反序列化第一個對象。
我該如何改變才能使程序反序列化所有對象?
但在我的情況下,我無法同時序列化所有對象,因爲最初的目的是在用戶在任何時候創建帳戶的應用程序中使用它(我的意思不是在同一時間) 任何除了序列化以外的其他選擇? – cyberpirate92
通過序列化,您需要將對象讀入內存,進行必要的更改(如添加用戶),然後將它們全部寫回到文件中。另一個簡單的方法是使用一個文本文件,例如CSV格式,每個用戶都有一行。更健壯的解決方案是使用數據庫(例如Java DB)。 – Kayaman
所以我必須反序列化所有對象,然後再次序列化它們在同一時間?但是這不會增加開銷嗎? – cyberpirate92