我正在从文本文件中读取两个字符串和一个双精度,但继续抛出一个EOFException
. 这是我的代码:
public static Book readBook(String pathname) throws IOException, FileNotFoundException{
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(fn)));
String theTitle = dis.readUTF();
String theAuthor = dis.readUTF();
double thePrice = dis.readInt();
dis.close();
return new Book(theTitle, theAuthor, thePrice);
}
我对 IO 真的很陌生,不知道如何解决这个异常,并且 throwingEOFExecption
似乎不起作用。任何帮助将不胜感激,干杯
编辑:堆栈跟踪 + 文件内容
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at Model.readBook(Model.java:48)
at Model.runApp(Model.java:17)
at Main.main(Main.java:8)
文件内容
姓名作者 10.00 姓名 2 作者 2 12.00
(在文件中,它们都在单独的行上)