我已经阅读了我在网上找到的任何页面,但其中没有一个适用于我。从Java中的二进制文件中读取字符串
我有一个用C代码创建的二进制文件。我也有这个二进制文件的C阅读器。我需要为这个二进制文件编写java reader。
在C代码中,以下命令读取一个大小为'b * max_w'的字符串和一个字符。
fscanf(f, "%s%c", &vocab[b * max_w], &ch);
在java中我读的二进制文件,
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bin = new BufferedInputStream(fis);
,然后读取字节并将其转换成字符串。
for(int j = 0; j < 200; j++) {
int size = 2; // char is 2 bytes
byte[] tempId3 = new byte[size];
bin.read(tempId3, 0, size);
String id3 = new String (tempId3);
System.out.println(" id = " + id3);
}
但是输出是一堆废话。难道我做错了什么?我可以做得更好吗?
编辑:从here从该运行的C片段是:
fscanf(f, "%lld", &words);
fscanf(f, "%lld", &size);
vocab = (char *)malloc((long long)words * max_w * sizeof(char));
for (a = 0; a < N; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
以下是我有:
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bin = new BufferedInputStream(fis);
int length = 1;
System.out.println("1st: ");
byte[] tempId = new byte[8];
bin.read(tempId, 0, 8);
String id = new String (tempId, "US-ASCII");
System.out.println(" out = " + id);
System.out.println("2nd: ");
int size1 = 8;
byte[] tempId2 = new byte[size1];
bin.read(tempId2, 0, size1);
String id2 = new String (tempId2, "US-ASCII");
System.out.println(" out = " + id2);
for(int j = 0; j < 20; j++) {
int size = 2;
byte[] tempId3 = new byte[size];
bin.read(tempId3, 0, size);
String id3 = new String (tempId3, "US-ASCII");
System.out.println(" out = " + id3);
}
,我看到的是下面的输出;除了前两个'长'数字,其余都是无稽之谈(预计会是字符)。
PS。 C代码是here(第44-60行是读取二进制文件的部分)
'new String(byte [])'构造函数使用系统的默认字符集进行解码。这可能是某种UTF-8,但它可能不是。尝试'System.out.println(System.getProperty(“file.encoding”));'找出它的设置。我很确定C使用ASCII作为字符(这将与UTF-8兼容),但我不是C程序员。另外,张贴一些废话。培训过的眼睛可能不是无稽之谈。 ; ) – Radiodef