java 逐字节读取二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25378938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading binary file byte by byte
提问by alwynmalan
I've been doing research on a java problem I have with no success. I've read a whole bunch of similar questions here on StackOverflow but the solutions just doesn't seem to work as expected.
我一直在研究 Java 问题,但没有成功。我已经在 StackOverflow 上阅读了一大堆类似的问题,但这些解决方案似乎没有按预期工作。
I'm trying to read a binary file byte by byte.
我正在尝试逐字节读取二进制文件。
I've used:
我用过:
while ((data = inputStream.read()) != -1)
loops...
循环...
for (int i = 0; i < bFile.length; i++) {
loops...
循环...
But I only get empty or blank output. The actual content of the file I'm trying to read is as follows:
但我只得到空或空白输出。我试图阅读的文件的实际内容如下:
?í sr assignment6.PetI?Z8kyQ? I ageD weightL namet Ljava/lang/String;xp > @4 t andysq ~ @bà t simbasq ~ @I t wolletjiesq ~
@$ t rakker
?í sr assignment6.PetI?Z8kyQ?I ageD weightL namet Ljava/lang/String;xp > @4 t andysq ~ @bà t simbasq ~ @I t wolletjiesq ~
@$ t rakker
I'm merely trying to read it byte for byte and feed it to a character array with the following line:
我只是想逐个字节地读取它并将其提供给具有以下行的字符数组:
char[] charArray = Character.toChars(byteValue);
Bytevalue here represents an int of the byte it's reading.
此处的 Bytevalue 表示它正在读取的字节的 int。
What is going wrong where?
哪里出了什么问题?
回答by Joop Eggen
Since java 7 it is not needed to read byte by byte, there are two utility function in Files:
由于 java 7 不需要逐字节读取,Files 中有两个实用程序函数:
Path path = Paths.get("C:/temp/test.txt");
// Load as binary:
byte[] bytes = Files.readAllBytes(path);
String asText = new String(bytes, StandardCharset.ISO_8859_1);
// Load as text, with some Charset:
List<String> lines = Files.readAllLines(path, StandardCharsets.ISO_8859_1);
As you want to read binary data, one would use readAllBytes
.
当您要读取二进制数据时,可以使用readAllBytes
.
String and char is for text. As opposed to many other programming languages, this means Unicode, so all scripts of the world may be combined. char
is 16 bit as opposed to the 8 bit byte
.
String 和 char 用于text。与许多其他编程语言相反,这意味着 Unicode,因此可以组合世界上所有的脚本。char
是 16 位而不是 8 位byte
。
For pure ASCII, the 7 bit subset of Unicode / UTF-8, byte and char values are identical.
对于纯 ASCII,Unicode / UTF-8 的 7 位子集,字节和字符值是相同的。
Then you might have done the following (low-quality code):
那么你可能做了以下(低质量的代码):
int fileLength = (int) path.size();
char[] chars = new char[fileLength];
int i = 0;
int data;
while ((data = inputStream.read()) != -1) {
chars[i] = (char) data; // data actually being a byte
++i;
}
inputStream.close();
String text = new String(chars);
System.out.println(Arrays.toString(chars));
The problem you had, probably concerned the unwieldy fixed size array in java, and that a char[]
still is not a String
.
您遇到的问题可能与 java 中笨拙的固定大小数组有关,并且 achar[]
仍然不是String
.
For binary usage, as you seem to be reading serialized data, you might like to dump the file:
对于二进制使用,当您似乎在读取序列化数据时,您可能希望转储文件:
int i = 0;
int data;
while ((data = inputStream.read()) != -1) {
char ch = 32 <= data && data < 127 ? (char) data : ' ';
System.out.println("[%06d] %02x %c%n", i, data, ch);
++i;
}
Dumping file position, hex value and char value.
转储文件位置、十六进制值和字符值。
回答by Joop Eggen
it is simple example:
这是一个简单的例子:
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
If you want to read text(characters) - use Readers, if you want to read bytes - use Streams
如果你想阅读文本(字符) - 使用阅读器,如果你想阅读字节 - 使用流
回答by Joop Eggen
Why not using Apache Commons:
为什么不使用 Apache Commons:
byte[] bytes = IOUtils.toByteArray(inputStream);
Then you can convert it to char:
然后您可以将其转换为字符:
String str = new String(bytes);
Char[] chars = str.toCharArray();
Or like you did:
或者像你一样:
char[] charArray = Character.toChars(bytes);
To deserialize objects:
要反序列化对象:
List<Object> results = new ArrayList<Object>();
FileInputStream fis = new FileInputStream("your_file.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
try {
while (true) {
results.add(ois.readObject());
}
} catch (OptionalDataException e) {
if (!e.eof) throw e;
} finally {
ois.close();
}
回答by DSOI__UNUNOCTIUM
Edit: Use file.length() for they array size, and make a byte array. Then inputstream.read(b). Edit again: if you want characters, use inputstreamreader(fileinputstream(file),charset), it even comes with charset.
编辑:使用 file.length() 作为数组大小,并制作一个字节数组。然后 inputstream.read(b)。再次编辑:如果你想要字符,使用 inputstreamreader(fileinputstream(file),charset),它甚至带有字符集。