在Java中,当使用DataOutputStream写入文件时,如何定义正在写入的数据的Endian?
我正在使用 DataOutputStream 写入文件,但是我想更改数据的字节序。
这就是我将字节数据写入文件的方式(默认情况下以 Little endian 输出)
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
有没有一种方法可以定义字节数据在 Java 中的写入方式?
I'm using DataOutputStream
to write to a file, however I want to change the endian of the data.
This is how i'm writing the byte data to the file (it outputs in Little endian 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 a way i can define the Endian of how byte data is written in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法使用
DataOutputStream
执行此操作,它始终使用大端字节序。您可以使用
ByteBuffer
您可以在其上调用order()
来影响它读取和写入数据的方式。您可以使用
ByteBuffer
either 来准备一个byte[]
,稍后您将使用经典的OutputStream
编写它或完全转到NIO并使用任何WritableByteChannel
用于写入You can not do this with
DataOutputStream
, which always uses big endian.You can use a
ByteBuffer
on which you can callorder()
to influence how it reads and writes data.You can use the
ByteBuffer
either to prepare abyte[]
that you'll write with a classicalOutputStream
later on or go entirely to NIO and use anyWritableByteChannel
for the writingcom.google.common.io.LittleEndianDataOutputStream
您可以使用:来自https://google.github.io/guava/releases/17.0/api/docs/com/google/common/io/LittleEndianDataOutputStream.html
或者复制这个类:
http://www.cafeaulait.org/books/javaio/ioexamples/ 07/LittleEndianOutputStream.java
You can use: com.google.common.io.LittleEndianDataOutputStream from
https://google.github.io/guava/releases/17.0/api/docs/com/google/common/io/LittleEndianDataOutputStream.html
or copy this class:
http://www.cafeaulait.org/books/javaio/ioexamples/07/LittleEndianOutputStream.java
您不能:
所有“多字节”方法都是这样工作的。
如果您需要相反的方式,则需要自己写入字节。
You cannot:
All the "multi-byte" methods work like that.
If you need it the other way around, you need to write bytes yourself.
所有给出的答案都是正确的。但是,您可以获取 DataOutputStream 的源,将其粘贴到新类中,并反转各种 writeShort、writeLong 等中的字节顺序(或者至少在您需要的字节顺序中)。这并不是一件困难的工作。
显然,您不能使用它与另一端的 DataInputStream 进行通信,但我想您需要使用另一端的 C 程序写入文件或套接字,因此您不需要 DataInputStream。
all the given answers are right. You can, however, take the source of DataOutputStream, paste it into a new class, and reverse the order of bytes in the various writeShort, writeLong etc.. (or at least in the ones you need). It is not such a difficult work.
Obviously you cannot then use it to communicate with a DataInputStream on the other side, but I suppose you need to write to a file or socket with a C program on the other side, so you'll not need a DataInputStream.
它以 DataInputStream 可读的方式输出数据。
如果您必须担心字节顺序,则不应使用 Data*Stream。
It outputs the data in a fashion that is readable by DataInputStream.
If you have to worry about the endianness, you should not be using a Data*Stream.