2013-05-31 43 views
0
Public static void main(String[] args) { 
    File inFile = null; 
    if (0 < args.length) { 
     inFile = new File(args[0]); 
    } 

    BufferedInputStream bStream = null; 

    try { 

     int read; 
     bStream = new BufferedInputStream(new FileInputStream(inFile)); 

     while ((read = bStream.read()) > 0) { 
      getMarker(read, bStream); 
      System.out.println(read); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

    finally { 
     try { 
      if (bStream != null)bStream.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

private static void getMarker(int read, BufferedInputStream bStream) { 

} 

我想在bufferedInputStream中找到長1234567890。我能夠搜索bufferedInputStream一個長類型? (我不確定是否需要'閱讀'作爲參數,我懷疑它,我可以刪除它)。如何搜索bufferedInputStream?Java,在二進制文件輸入中搜索一長

+1

「長」字大小爲8個字節。你的文件是8字節對齊的嗎? – fge

+0

更重要的是,根據你的文件中的長文件是寫成小端還是大端,它的二進制表示有所不同。那麼,什麼是什麼? – fge

回答

0

如果數字是8字節對齊且大端,則可以使用DataInputStream。

如果不是,我建議內存映射文件,並使用ByteBuffer.order更改順序爲little endian。這將允許您以任何您希望的方式訪問該文件。