对于我的应用程序,我想使用Map充当数据库.为了保存和加载地图,我正在使用以下2种方法将其写入database.ser中/从中读取:
For my application I want to use a Map to act as a database. To save and load a map, I am writing/reading it to/from database.ser using this 2 methods:
private synchronized void saveDB() { try { fileOut = new FileOutputStream(db); out = new ObjectOutputStream(fileOut); out.writeObject(accounts); fileOut.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") private void loadDB() { try { fileIn = new FileInputStream(db); in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty accounts = (Map<String, Client>) in.readObject(); in.close(); fileIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }我想在应用程序启动时加载到Map中,所以我在构造函数中调用这样的方法:
I want to load into Map when application starts, so I invoke method in constructor like this:
protected DriveatorImpl() { accounts = new ConcurrentHashMap<String, Client>(); db = new File("C:/Users/eduar/git/Multy-Threaded-Bank-System/Bank-Services/database.ser"); // also, any suggestions how can I make path to a file more flexible in case I want to run Server side of an app on different machine? if (!db.exists()) { try { db.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } loadDB(); // loads database when server start }我知道引起错误的原因,但我不知道我应该在设计中进行哪些更改,以避免ObjectInputStream构造函数接收空流! 关于我可以做些什么的任何建议?
I am aware of what causing an error, but I don't know what should I change in my design to avoid ObjectInputStream constructor receiving empty stream! Any suggestions on what I can do differently?
我想指出,在新应用程序中运行database.ser为空,因为尚未在Map中进行任何输入.
I want to note that in fresh application run database.ser is empty since there was no entries made into Map yet.
谢谢!
推荐答案为什么会出现EOFExcpetion?
First why the EOFExcpetion occur?
文件中没有内容,或者文件为空,您尝试读取文件.
There are no contents in file or file is empty and you tried to read file.
- 您可以通过检查文件内容的长度是否小于或等于零(表示文件为空)来避免空文件出现EOFException. 另一种检查文件是否为空的方法
某些代码更改对我有用.
Some code change and it worked for me.
@SuppressWarnings("unchecked") private void loadDB() { try { if (db.length() <= 0) { // if statement evaluates to true even if file doesn't exists saveDB(); // save to a file an empty map // if file doesn't exist, it creates a new one // call loadDB inside constructor } FileInputStream fileIn = new FileInputStream(db); ObjectInputStream in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty in.readObject(); in.close(); fileIn.close(); System.out.println(accounts); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }更多推荐
解决方法java.io.EOFException由ObjectInputStream引起
发布评论