2013-04-18 26 views
0

我想將ByteBuffer作爲Little Endian導出到文件中,但是當我再次讀入文件時,必須將其作爲Big Endian讀取以獲取正確的值。我怎樣才能以一種可以在創建的文件中讀取Little Endian並獲取正確值的方式導出ByteBuffer?將ByteBuffer導出爲Java中的Little Endian文件

//In the ByteBuffer tgxImageData there are the bytes which I want to export and import again, the ByteBuffer is ordered as little endian 


    //export ByteBuffer into File 
    FileOutputStream fos = new FileOutputStream(outputfile);    
    tgxImageData.position(0); 
    byte[] tgxImageDataByte = new byte[tgxImageData.limit()]; 
    tgxImageData.get(tgxImageDataByte);   
    fos.write(tgxImageDataByte);    
    fos.close(); 


    //import File into ByteBuffer 
    FileInputStream fis2 = new FileInputStream(outputfile);  
    byte [] arr2 = new byte[(int)outputfile.length()]; 
    fis2.read(arr2); 
    fis2.close();   
    ByteBuffer fileData2 = ByteBuffer.wrap(arr2); 


    fileData2.order(ByteOrder.LITTLE_ENDIAN);   
    System.out.println(fileData2.getShort(0));   //Wrong output, but here should be right output 
    fileData2.order(ByteOrder.BIG_ENDIAN);   
    System.out.println(fileData2.getShort(0));   //Right output, but here should be wrong output 
+0

哪裏是哪裏,你在小尾數_write_的一部分嗎?我只看到你寫了一個字節[]。寫入字節[]的值是否已被寫入小端?那會進入tgxImageData,我猜? – Fildor

回答

0

這是怎麼寫的短褲爲小尾數爲例

FileOutputStream out = new FileOutputStream("test"); 
    ByteBuffer bbf = ByteBuffer.allocate(4); 
    bbf.order(ByteOrder.LITTLE_ENDIAN); 
    bbf.putShort((short)1); 
    bbf.putShort((short)2); 
    out.write(bbf.array()); 
    out.close(); 

你需要做這樣的事情在你的代碼