在java中序列化ArrayList
这是我第一次体验序列化。我的数据在arraylist中。当我反序列化它时,arraylist是空的,我不知道为什么。arraylist只包含Account类的两个对象(但将来可能会包含更多)。Account类是可序列化的,因为我相信ArrayList对象就是这样。这是我的密码:在java中序列化ArrayList,java,serialization,arraylist,deserialization,Java,Serialization,Arraylist,Deserialization,这是我第一次体验序列化。我的数据在arraylist中。当我反序列化它时,arraylist是空的,我不知道为什么。arraylist只包含Account类的两个对象(但将来可能会包含更多)。Account类是可序列化的,因为我相信ArrayList对象就是这样。这是我的密码: public class BankDatabase implements Serializable { public ArrayList<Account> accounts; private st
public class BankDatabase implements Serializable
{
public ArrayList<Account> accounts;
private static ObjectOutputStream output;
private static ObjectInputStream input;
public BankDatabase()
{
accounts = new ArrayList();
File database = new File("database.ser");
if(!database.exists())
{
Account testAccount[] = new Account[2];
testAccount[0] = new Account(12345, 54321, 1000.0, 1200.0);
testAccount[1] = new Account(98765, 56789, 200.0, 200.0);
accounts.add(0, testAccount[0]);
accounts.add(1, testAccount[1]);
}
else
loadDatabase();
}
public static void openIn()
{
try
{
input = new ObjectInputStream(Files.newInputStream(Paths.get("database.ser")));
}
catch (IOException ioException)
{
System.err.println("Could not open file. Closing Program.");
System.exit(1);
}
}
public static void openOut()
{
try
{
output = new ObjectOutputStream(Files.newOutputStream(Paths.get("database.ser")));
}
catch (IOException ioException)
{
System.err.println("Could not open file. Closing Program.");
System.exit(1);
}
}
public void readData()
{
try
{
while(true)
{
accounts.add((Account) input.readObject());
}
}
catch (EOFException endOfFileException)
{
System.out.printf("No More Records%n");
}
catch (ClassNotFoundException classNotFoundException)
{
System.err.println("Invalid object type.");
}
catch (IOException ioException)
{
System.err.println("Could not read file.");
}
}
public void saveData()
{
try
{
for(Account currentAccount : accounts)
{
output.writeObject(currentAccount);
}
}
catch (IOException ioException)
{
System.err.println("Error writing to file. Terminating");
}
}
public void closeOut()
{
try
{
if (output != null)
output.close();
}
catch (IOException ioException)
{
System.err.println("Error closing file. Terminating.");
System.exit(1);
}
}
public void closeIn()
{
try
{
if (input != null)
input.close();
}
catch (IOException ioException)
{
System.err.println("Error closing file. Terminating.");
System.exit(1);
}
}
public void loadDatabase()
{
openIn();
readData();
closeIn();
}
public void updateDatabase()
{
openOut();
saveData();
closeOut();
}
}
public class Account implements Serializable
{
public int accountNumber;
public int pin;
public double availableBalance;
public double totalBalance;
public Account(int theAccountNumber, int thePIN,
double theAvailableBalance, double theTotalBalance)
{
accountNumber = theAccountNumber;
pin = thePIN;
availableBalance = theAvailableBalance;
totalBalance = theTotalBalance;
}
公共类数据库实现可序列化
{
公共ArrayList账户;
私有静态ObjectOutputStream输出;
私有静态ObjectInputStream输入;
公共银行数据库()
{
accounts=newarraylist();
文件数据库=新文件(“database.ser”);
如果(!database.exists())
{
账户testAccount[]=新账户[2];
testAccount[0]=新账户(12345543211000.01200.0);
testAccount[1]=新账户(9876556789200.00200.0);
添加(0,testAccount[0]);
增加(1,测试账户[1]);
}
其他的
loadDatabase();
}
公共静态void openIn()
{
尝试
{
input=newObjectInputStream(Files.newInputStream(path.get(“database.ser”)));
}
捕获(IOException IOException)
{
System.err.println(“无法打开文件。正在关闭程序”);
系统出口(1);
}
}
公共静态void openOut()
{
尝试
{
output=newobjectoutputstream(Files.newOutputStream(path.get(“database.ser”)));
}
捕获(IOException IOException)
{
System.err.println(“无法打开文件。正在关闭程序”);
系统出口(1);
}
}
public void readData()
{
尝试
{
while(true)
{
accounts.add((Account)input.readObject());
}
}
捕获(EOFEException EndofileException)
{
System.out.printf(“不再有记录%n”);
}
捕获(ClassNotFoundException ClassNotFoundException)
{
System.err.println(“无效对象类型”);
}
捕获(IOException IOException)
{
System.err.println(“无法读取文件”);
}
}
公共void saveData()
{
尝试
{
对于(帐户当前帐户:帐户)
{
output.writeObject(当前帐户);
}
}
捕获(IOException IOException)
{
System.err.println(“写入文件时出错。正在终止”);
}
}
公开作废收尾()
{
尝试
{
if(输出!=null)
output.close();
}
捕获(IOException IOException)
{
System.err.println(“关闭文件时出错。正在终止”);
系统出口(1);
}
}
公共空间关闭()
{
尝试
{
如果(输入!=null)
input.close();
}
捕获(IOException IOException)
{
System.err.println(“关闭文件时出错。正在终止”);
系统出口(1);
}
}
公共数据库()
{
openIn();
readData();
closeIn();
}
public void updateDatabase()
{
openOut();
saveData();
收尾();
}
}
公共类帐户实现可序列化
{
公共国际帐户号码;
公共int pin;
公共双可用平衡;
公共双总量平衡;
公共帐户(输入帐号,输入密码,
可用余额加倍,总余额加倍)
{
accountNumber=帐户编号;
针=针;
可用余额=可用余额;
总平衡=总平衡;
}
ArrayList是可序列化的,执行ObjectOutput.writeObject(list)
保存它,执行list list=(list)objectInputStream.readObject()
回读从未被调用。不要同时打开文件进行读写。@Robby这不是全部代码。包含所有代码太多了。我有一个名为存款和取款的类,它调用updateDatabase()@eckes代码已编辑,但仍存在相同问题。还意识到我没有关闭输入流。现在看起来不错,而且您没有调用更新/保存方法。第一次保存后文件有多大?请确保您没有使用旧的文件,因为旧的文件可能会被旧的尝试破坏。