4). 最簡單的方式是,使用我編寫的LEDataInputStream, LEDataOutputStream 和LERandomaccessFile模擬 DataInputStream, DataOutputStream and RandomAccessFile ,它們使用的是little-endian字節(jié)流。 You can read about LEDataStream. You can download the code and source free. You can get help from the File I/O Amanuensis to show you how to use the classes. Just tell it you have little-endian binary data.
2.你可能甚至不會(huì)有任何問題。 從C來的許多Java新手可能會(huì)認(rèn)為需要考慮它們所依靠的平臺(tái)內(nèi)部所使用的是big還是little問題。在Java中這不是一個(gè)問題。進(jìn)一步,不借助于本地類,你無法知道它們是如何存儲(chǔ)的。Java has no strUCt I/O and no unions or any of the other endian-sensitive language constructs.
僅在與遺留的C/C++應(yīng)用程序通訊時(shí)需要考慮endian問題。下列代碼在big or little endian機(jī)器上都將產(chǎn)生同樣的結(jié)果:
// take 16-bit short apart into two 8-bit bytes. short x = 0xabcd; byte high = (byte) (x >>> 8); byte low = (byte) x;/* cast implies & 0xff */ System.out.PRintln ("x=" + x + " high=" + high + " low=" + low );
3.讀Little-Endian Binary Files The most common problem is dealing with files stored in little-endian format.
I had to implement routines parallel to those in java.io.DataInputStream which reads raw binary, in my LEDataInputStream and LEDataOutputStream classes. Don't confuse this with the io.DataInput human-readable character-based file-interchange format.
If you wanted to do it yourself, without the overhead of the full LEDataInputStream and LEDataOutputStream classes, here is the basic technique:
Presuming your integers are in 2's complement little-endian format, shorts are pretty easy to handle: