[英]Reading data from binary file correctly in Java
我在讀取理解的數據時遇到問題,當我使用解析(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();
}
結果:
(2 <FPJ ava
我期望的結果是:10 20 30 40 50 60 70 80 J ava
我嘗試使用(byte)代替(char),同樣的問題,但是Java單詞變成了ASCII中的數字,請幫忙嗎?
byte []數組中的數字被解釋為字節值。 您必須編寫Interger並將其轉換為數組內的字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.