我正在尝试读取 csv 文件并在控制台中显示输出。但是,我不明白为什么我得到文件结束异常并且控制台中没有显示任何内容?
异常消息是:
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readUTF(DataInputStream.java:609)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at ByteIO.main(ByteIO.java:17)
我的代码是:
import java.io.*;
import javax.swing.*;
public class ByteIO
{
public static void main(String[] args)
{
try
{
FileInputStream fis = new FileInputStream("BaseballNames1.csv");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
while(dis.available() > 0)
{
String header = (dis.readUTF());
String firstName = (dis.readUTF());
String lastName = (dis.readUTF());
String fullName = (firstName + " " + lastName);
int birthDay = (dis.readInt());
int birthMonth = (dis.readInt());
int birthYear = (dis.readInt());
String birthDate = (birthMonth + "/" + birthDay + "/" + birthYear);
int weight = (dis.readInt());
double height = (dis.readDouble());
System.out.println( header + fullName + birthDate + weight + height);
dis.close();
}
}
catch(EOFException eof)
{
System.out.println("End of File!");
}
catch(FileNotFoundException fe)
{
System.out.println("File not found!");
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
}