在Java中将ByteBuffer导出为小端文件
我想将ByteBuffer作为小尾端导出到文件中,但当我再次读取文件时,我必须将其作为大尾端来读取,以获得正确的值。如何导出ByteBuffer,使其在创建的文件中以Little Endian的形式读取并获得正确的值在Java中将ByteBuffer导出为小端文件,java,import,export,endianness,bytebuffer,Java,Import,Export,Endianness,Bytebuffer,我想将ByteBuffer作为小尾端导出到文件中,但当我再次读取文件时,我必须将其作为大尾端来读取,以获得正确的值。如何导出ByteBuffer,使其在创建的文件中以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
//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
这是一个如何编写短片的例子
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();
您需要在代码中执行类似的操作用little endian编写的部分在哪里?我只看到你在写一个字节[]。写入字节[]的值是否以小尾端形式写入?我想那会是tgxImageData吗?