【发布时间】:2014-08-25 12:10:47
【问题描述】:
我正在考虑一种方法将从文本文件中读取的一堆数据存储到一个数组列表中。在我阅读文件后,我必须知道如何存储它。例如,来自文本文件的数据: 用 C++ 解决问题:Walter Savitch:9780132773348:QA760-073:NON FICTION:2 这是我的部分代码:
从文本文件中获取:
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text and Data Files", "txt", "dat");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(MainMenu.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filename = chooser.getSelectedFile()
.getAbsolutePath();
readBooks(filename);
}
}
public void readBooks(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
int i = 0;
String line;
line = reader.readLine();
while (line != null && !line.equals("")) {
i++;
processBook(line);
line = reader.readLine();
}
System.out.println("" + i + " books read");
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public void processBook(String line) {
int id = nextBookID;
nextBookID++;
String[] parts = line.split(":");
String title = parts[0];
String author = parts[1];
String bookISBN = parts[2];
String callNumber = parts[3];
String type = parts[4];
String noOfCopy = parts[5];
System.out.println("Creating book " + id);
// TODO: add create book and add to collection(s)
}
【问题讨论】:
-
收藏什么??字符串??
-
可能我想设置一个数组列表