现在我正在尝试编写一个读取.cdr文件的程序。cdr文件的格式如下:
***************
* File header *
***************
File length: 44142 bytes
Header length: 54 bytes
High release identifier: Check the field extension below
High version identifier: Version 6
Low release identifier: Check the field extension below
Low version identifier: Version 6
File opening local timestamp: May 20 00:28 (+02:00 from UTC)
Timestamp of last CDR on file: May 20 01:28 (+02:00 from UTC)
Number of CDRs in the file: 130 CDRs
File sequence number: 386
File closure trigger reason: File open-time limit reached
IP address of node generating the file: ff.ff.ff.ff::ffff:ac10:1438
Lost CDR indicator: 0 CDRs lost
Length of CDR routeing filter: 0 bytes
Length of private extensions: 0 bytes
High release ID extension: Release 13
Low release ID extension: Release 13
这是我的代码:
readBinary方法:
public Collection<Data> readBinary(String file) throws IOException, FileNotFoundException
{
ArrayList<Data> arrayList=new ArrayList<>();
try(DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream(file))))
{
while (dis.available()>0)
{
arrayList.add(new Data(dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(),dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readInt(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readUTF()));
}
}
return arrayList;
}
main:
public class Main implements Serializable
{
public static void main(String[] args) throws IOException, ClassNotFoundException {
Methoden m = new Methoden();
try {
ArrayList<Data> d1 = (ArrayList<Data>) m.readBinary("172.16.20.5601_-_386.20210520_-_0128+0200.cdr");
System.out.println(d1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
所以我的问题是,当我运行程序时,我会得到一个EOFException,我不知道为什么。。
也许有人可以帮我解决问题
首先需要读取的是文件头中的文件长度字段。这可能是一个字节,也可能是两个字节——请检查您的规范。看起来您正在使用DataInputStream.readUTF读取,这不适合读取这样的字段。
你需要根据文件布局进行阅读。您似乎没有遵循文件布局,因此碰巧试图读取比您应该读取的更多的数据,因此出现了异常。