二进制 I/O 跳过字节并仅打印 UTF 类型
Binary I/O Skipping the bytes and printing only the UTF types
所以这是我的问题:我需要从 .dat 文件中读取一些数据,问题是并不是所有的东西都保存相同(一些 UTF、Int、Double),所以我不能 readUTF()
在一个循环中直到它完成,因为它会偶然发现一个 Int 并给我一个错误。我知道的一件事是 .dat 文件中的内容顺序,它们是这样的:UTF、Int、Double、Double、Double。这是我到目前为止的代码:
import java.io.*;
public class BytePe1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("ClassList.dat");
BufferedInputStream bis = new BufferedInputStream( fis );
DataInputStream dis = new DataInputStream(bis);
String studentName;
int studentNumber;
//while(dis.readLine() != null) {
System.out.println("Name");
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readDouble());
System.out.println(dis.readDouble());
//System.out.println(dis.readUTF());
//And I would need to repeat these steps above but I don't know how many
//Files there actually are, so I would like to not just spam this until I see errors
//}
dis.close();
}
catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
这将输出正确的内容,但我不知道我在该文件中保存了多少内容,这就是我想知道的;是否可以跳过文件的某些部分并只打印所有名称,然后打印 int 等等。
One small part of the reading
Java 的 RandomAccessFile has two useful methods, getFilePointer() and length()。只要getFilePointer()小于length(),就有数据可读。
try {
RandomAccessFile raf = new RandomAccessFile("ClassList.dat", "r");
while (raf.getFilePointer() < raf.length()) {
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
System.out.println(raf.readDouble());
System.out.println(raf.readDouble());
System.out.println(raf.readDouble());
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
所以这是我的问题:我需要从 .dat 文件中读取一些数据,问题是并不是所有的东西都保存相同(一些 UTF、Int、Double),所以我不能 readUTF()
在一个循环中直到它完成,因为它会偶然发现一个 Int 并给我一个错误。我知道的一件事是 .dat 文件中的内容顺序,它们是这样的:UTF、Int、Double、Double、Double。这是我到目前为止的代码:
import java.io.*;
public class BytePe1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("ClassList.dat");
BufferedInputStream bis = new BufferedInputStream( fis );
DataInputStream dis = new DataInputStream(bis);
String studentName;
int studentNumber;
//while(dis.readLine() != null) {
System.out.println("Name");
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readDouble());
System.out.println(dis.readDouble());
//System.out.println(dis.readUTF());
//And I would need to repeat these steps above but I don't know how many
//Files there actually are, so I would like to not just spam this until I see errors
//}
dis.close();
}
catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
这将输出正确的内容,但我不知道我在该文件中保存了多少内容,这就是我想知道的;是否可以跳过文件的某些部分并只打印所有名称,然后打印 int 等等。 One small part of the reading
Java 的 RandomAccessFile has two useful methods, getFilePointer() and length()。只要getFilePointer()小于length(),就有数据可读。
try {
RandomAccessFile raf = new RandomAccessFile("ClassList.dat", "r");
while (raf.getFilePointer() < raf.length()) {
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
System.out.println(raf.readDouble());
System.out.println(raf.readDouble());
System.out.println(raf.readDouble());
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}