在person.txt中,我们存储了该人员的详细信息。 看起来像这样。文件读取器直到结尾才读取所有数据
John
Smith
aösldkjf
5
8645
asdfasf
0441234545
++++++
Adam
Gilchrist
ads
asf
asf
asfd
0441234546
++++++
然后我们建立了FileManager类来读取这个文件中的数据。 它标识有两个不同的条目。 但它始终读取前8行,不会继续。由于第一个人(例如: - John Smith)被添加了两次到名为AddressBook的“LinkedList”。
//文件管理器类
public class FileManager {
public static void readFile() {
Scanner x;
LinkedList<String> tempList = new LinkedList<String>();
try {
x = new Scanner(new File("Person.txt"));
@SuppressWarnings("unused")
String temp = null;
while (x.hasNext()) {
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
tempList.add(x.next());
Person person = new Person();
person.addFilePerson(tempList);
Main.addressBook.add(person);
}
} catch (Exception e) {
System.out.println("could't find the file");
}
}
}
// addFilePerson方法的类人
public void addFilePerson(LinkedList<String> list){
vorname = list.get(0);
nachname = list.get(1);
strasse = list.get(2);
hausnummer = list.get(3);
plz = list.get(4);
telefon = list.get(5);
wohnort = list.get(6);
}
的代码只是一行。完美的作品。 – 2012-04-15 17:23:36