在 Java 中,使用 DataOutputStream 写入文件时,如何定义正在写入的数据的 Endian?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7024039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:19:53  来源:igfitidea点击:

In Java, when writing to a file with DataOutputStream, how do I define the Endian of the data being written?

javaioendianness

提问by Zeeno

I'm using DataOutputStreamto write to a file, however I want to change the endian of the data.

我正在使用DataOutputStream写入文件,但是我想更改数据的字节序。

This is how i'm writing the byte data to the file (it outputs in Little endian by default)

这就是我将字节数据写入文件的方式(默认情况下它以 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

Is there a way i can define the Endian of how byte data is written in Java?

有没有一种方法可以定义字节数据在 Java 中的写入方式?

回答by Joachim Sauer

You can not do this with DataOutputStream, which always uses big endian.

你不能用 来做到这一点DataOutputStream,它总是使用大端。

You canuse a ByteBufferon which you can call order()to influence how it reads and writes data.

可以使用ByteBuffer可以调用的 aorder()来影响它读取和写入数据的方式。

You can use the ByteBuffereitherto prepare a byte[]that you'll write with a classical OutputStreamlater on orgo entirely to NIO and use any WritableByteChannelfor the writing

您可以使用ByteBuffer任一来准备byte[],你会用一个经典的写OutputStream上后完全去NIO和使用任何WritableByteChannel的写作

回答by Thilo

You cannot:

你不能

Writes a short to the underlying output stream as two bytes, high byte first.

将 short 写入底层输出流作为两个字节,高字节在前。

All the "multi-byte" methods work like that. If you need it the other way around, you need to write bytes yourself.

所有的“多字节”方法都是这样工作的。如果反过来需要它,则需要自己编写字节。

回答by njzk2

It outputs the data in a fashion that is readable by DataInputStream.

它以 DataInputStream 可读的方式输出数据。

If you have to worry about the endianness, you should not be using a Data*Stream.

如果您必须担心字节顺序,则不应使用 Data*Stream。

回答by Simone Gianni

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.

所有给出的答案都是正确的。但是,您可以获取 DataOutputStream 的源,将其粘贴到一个新类中,然后颠倒各种 writeShort、writeLong 等(或至少在您需要的那些)中的字节顺序。这不是一件困难的工作。

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 进行通信,但我想您需要在另一端使用 C 程序写入文件或套接字,因此您不需要 DataInputStream。