我正在使用此代码读取 Java 文件
import java.io.*;
public class IOReadDataStreams {
public static void main(String[] args)throws IOException{
DataInputStream in = null;
try{
in = new DataInputStream(
new BufferedInputStream(new FileInputStream("invoicedata")));
int unit;
double price;
String desc;
while(true){
unit = in.readInt();
price = in.readDouble();
desc = in.readUTF();
System.out.println(unit+" " + price +" "+ desc);
}
}catch(EOFException e){
e.printStackTrace();
}
finally{
if(in != null)
in.close();
}
}
}
我试图阅读的文件是由这段代码制作的
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
public class IODataStreams {
static final String dataFile = "invoicedata";
static final double[] prices = new double[]{19.99, 9.99,15.99,3.99,4.99};
static final int[] unitCount = new int[]{12,8,13,29,50};
static final String[] desc = new String[]{
"Java T Shirt",
"C# T Shirt",
"PHP T Shirt",
"Ruby T Shirt",
"Go! T Shirt"
};
public static void main(String[] args) throws IOException{
DataOutputStream out = null;
DataInputStream in = null;
try{
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("invoicedata")));
for(int i = 0 ; i < prices.length; i++){
out.writeUTF(desc[i]);
out.writeInt(unitCount[i]);
out.writeDouble(prices[i]);
}
}finally{
if(out != null)
out.close();
}
}
}
现在我想知道为什么我的第一个代码(读取文件的那个)没有打印价格、描述和单位。它继续返回 EOFException
错误专门在这一行
desc = in.readUTF();