从文件读取信息到链接列表 [英] Reading information from a file to a linked list
本文介绍了从文件读取信息到链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经获得了这段代码,用于从文件中读取信息并将其加载到链接的
节点列表.但是下面的代码崩溃了:-(
I''ve got this code for reading information from a file and load it into a linked
list of nodes. but the code below crashes :-(
if (btn == btnLoad)
{
try
{
booklist.clear();
BufferedReader reader = new BufferedReader(new FileReader("BOOKDATA.txt"));
String line= reader.readLine();
Books ObjBook = new Books();
while(line!=null)
{
String[] ObjectArray =line.split(" ");
Books BookObject=new Books(ObjectArray[0],ObjectArray[1],Double.parseDouble(ObjectArray[2]),Integer.parseInt(ObjectArray[3]),Integer.parseInt(ObjectArray[4]),ObjectArray[5],ObjectArray[6],ObjectArray[7]);
booklist.add(BookObject);
line = reader.readLine();
}
reader.close();
}
catch(EOFException eof)
{
System.out.println("End of file");
}
catch(FileNotFoundException fnf)
{
System.out.println("File Not Found");
}
catch(IOException error)
{
System.out.println("IO error");
}
}
推荐答案
请确保首先从Object数组转换这些值,然后再使用它们-进行验证:
Please make sure to first convert those values from the Object array and afterwards to use them - with a validation:
// short form
Book oBook = new Book(
value == null ? alternateValue:originalValue,
args2,
args3
);
这篇关于从文件读取信息到链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文