In Java, how to define the endian of the data being written when using dataoutputstream to write a file?
•
Java
I am using dataoutputstream to write to the file, but I want to change the endian of the data
This is how I write byte data to a file (output as little end by default)
public void generateBinObjFile(String outputFile) try { // Create file DataOutputStream stream = new DataOutputStream( new FileOutputStream(outputFile)); stream.writeShort(this.quantize(this.xComponents.get(index),//<-- Short is written in little Endian this.min_x,this.max_x) - 32768); } // catch statements here
Is there any way to define endian written in Java for byte data?
Solution
You can't use dataoutputstream to do this. It always uses the big end
You can use ByteBuffer to call order () to affect how it reads and writes data
You can use ByteBuffer to prepare a byte [], which you will write later using the classic OutputStream or go completely to NiO and write using any writablebytechannel
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码