[英]Reading data from binary file correctly in Java
I have problem with reading understood data, when I used Parsing (char) the numbers became strange char in ASCII, here is the code : 我在读取理解的数据时遇到问题,当我使用解析(char)时,数字变成了ASCII中的奇怪char,这是代码:
static void doTestByteFiles() throws IOException {
File file = new File("sample1.data");
FileOutputStream outStream = new FileOutputStream(file); //Warning!!!!
byte[] outByteArray = {10,20,30,40,50,60,70,80,(byte)'J',(byte)'a',(byte)'v',(byte)'a'};
outStream.write(outByteArray);
outStream.close();
FileInputStream inStream = new FileInputStream(file);
int fileSize = (int) file.length();
byte[] inByteArray = new byte[fileSize];
inStream.read(inByteArray);
for (int i = 0; i < fileSize; i++) {
System.out.println((char) inByteArray[i]);
}
inStream.close();
}
the result: 结果:
( 2 < FPJ ava (2 <FPJ ava
result I expect : 10 20 30 40 50 60 70 80 J ava 我期望的结果是:10 20 30 40 50 60 70 80 J ava
I tried to use (byte) instead (char), same problem but the Java word became numbers in ASCII , Any help please ? 我尝试使用(byte)代替(char),同样的问题,但是Java单词变成了ASCII中的数字,请帮忙吗?
The numbers in the byte[] array are interpreted as byte values. byte []数组中的数字被解释为字节值。 You have to write Intergers and convert them to byte inside the array. 您必须编写Interger并将其转换为数组内的字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.