2015-04-25 65 views
0

我正在使用下面的代碼從「.264」文件中讀取數據。從文件中讀取字節

public static void main (String[] args) throws IOException 
{ 

    BufferedReader br = null;try { 
     String sCurrentLine; 
     br = new BufferedReader(new InputStreamReader(new FileInputStream("test.264"),"ISO-8859-1")); 
     StringBuffer stringBuffer = new StringBuffer(); 
     while ((sCurrentLine = br.readLine()) != null) { 
      stringBuffer.append(sCurrentLine);  
     } 
     String tempdec = new String(asciiToHex(stringBuffer.toString())); 
     System.out.println(tempdec); 
     String asciiEquivalent = hexToASCII(tempdec); 
     BufferedWriter xx = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator/Desktop/yuvplayer-2.3/video dinalized/testret.264"),"ISO-8859-1")); 
     xx.write(asciiEquivalent); 
     xx.close(); 
    }catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (br != null)br.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

} 

在十六進制編輯器中打開輸入和輸出文件會顯示一些缺失值,例如: 0d(見附圖)。

任何解決方案來解決這個問題?

image1 image2

+0

您在文本模式下讀取二進制文件。在後者中,行由'0d'字符(CR)分隔。 – mins

+0

如何正確讀取它? –

+2

簡短的答案是*你打開它作爲一個字節流*,這裏有一個例子(它存儲在內存中的字節,你需要把它們寫入一個文件):[文件到字節\ [\]在Java]( http://stackoverflow.com/questions/858980/file-to-byte-in-java)。但除此之外,您應該明確自己的需求,其他解決方案可能會得到更好的優化。 – mins

回答

1

失去InputStreamReaderBufferedReader,只是對自己使用FileInputStream
沒有字符編碼,沒有行尾,只是字節。
它的Javadoc頁面是here,應該是你所需要的。

提示:如果你想閱讀整個文件一次:File.length(),如

File file = new File("test.264"); 
byte[] buf = new byte[(int)file.length()]; 
// Use buf in InputStream.read()