我試圖做一個程序,存儲一個類的數組我已經表示爲條目。該類根據命令,名稱,編號和筆記存儲用戶的輸入。收到條目後,它將通過ObjectInput/ObjectOutput發送到一個文件,並存儲以供以後使用。未知原因獲取運行時錯誤;引用ObjectInputStream
有點像一個簡單的聯繫人列表,可以定期調用和不時更新。
我遇到了一種奇怪的錯誤(見下文)。如果有人可以幫助我解決這個錯誤,我將不勝感激。
代碼:
import java.io.*;
import java.util.Scanner;
public class phonebook {
protected Entry[] entryList = new Entry[200];
protected int length = 0;
public void doList() {
for (int i=0; i<length; i++) {
System.out.print(entryList[i]);
}
}
public void doAddEntry(Entry entry) throws Exception {
if (length == 200) {
throw new Exception("I'm full");
}
for (int i = 0; i<length; i++) {
if (entryList[i].name.compareToIgnoreCase(entry.name) <0) {
//?
}
}
}
public void doFind(String term) {
// look for input in entryList's name fields
}
public void doSave() throws Exception {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("contacts.txt"));
os.writeObject(entryList);
os.close();
}
public void doLoad() throws Exception {
***ObjectInputStream oin = new ObjectInputStream(new FileInputStream("contacts.txt"));***
entryList = (Entry[])oin.readObject();
oin.close();
}
public static void main(String[] args) throws Exception {
String line;
String[] command;
Scanner input;
String delimeter;
delimeter = " ";
phonebook pbook;
String cmd;
String term;
pbook = new phonebook();
cmd= "";
input=new Scanner (System.in);
**pbook.doLoad();**
System.out.println("Codes are entered as 1 to 8 characters");
System.out.println("Use \"e\" for enter, \"f\" for find, \"l\" to list, and \"q\" to quit");
System.out.println();
do {
System.out.print("Command: ");
line = input.nextLine();
command = line.split(delimeter);
if (command[0].equalsIgnoreCase("e")) {
Entry e = new Entry();
e.name = command[0];
System.out.print("Enter Number: ");
e.number=input.nextLine();
System.out.print("Enter Notes: ");
e.notes=input.nextLine();
System.out.println("");
pbook.doAddEntry(e);
} else if (command[0].equalsIgnoreCase("f")) {
term=command[0];
pbook.doFind(term);
} else if (command[0].equalsIgnoreCase("l")) {
pbook.doList();
} else if (command[0].equalsIgnoreCase("q")) {
System.out.println("Exiting Program...");
pbook.doSave();
break;
} else {
System.out.println("Invalid Command");
continue;
}
} while (true);
}
}
類別:
public class Entry implements java.io.Serializable {
public String name;
public String number;
public String notes;
}
編譯器錯誤消息(可以發現代碼引用錯誤其間*以上):
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at phonebook.doLoad(phonebook.java:39)
at phonebook.main(phonebook.java:57)
這不是編譯器錯誤,它是運行時錯誤。看起來像contacts.txt是空的。 – 2013-04-04 17:07:24
@MarkRotteveel&Perception感謝傢伙我會更新標題,並且可以確認我沒有發胖,當我的代碼粘貼到原始帖子時,我遇到了一個問題。 – Tdorno 2013-04-04 17:21:31
@MarkRotteveel contacts.txt是空的,因爲我沒有放入任何東西。該程序應該輸入並將其存儲在文件中供以後使用,但不起作用。 – Tdorno 2013-04-04 17:22:53