在Java中编写文件时如何避免额外的头字节? [英] How to avoid extra header bytes while writing a file in Java?

查看:192
本文介绍了在Java中编写文件时如何避免额外的头字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我没有使用高级默认的Java序列化来编写文件中的对象。我手动在文件中编写一些原始类型变量。下面是示例:

Firstly, I am not using high-level default serialization of Java to write an object in a file. I am manually writing some primitive type variables in a file. Here is the example:

public class TestMain {
    public static void main(String[] args) {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("Test.bin")));

            out.writeInt(1024);
            out.writeInt(512);
            out.writeInt(256);
            out.writeInt(128);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

现在我测试了创建的文件Test.bin。 filesize是 22个字节。但是计算我在文件中实际写入的内容,文件大小应该是 16字节。 [ int 各为4个字节。 4 int 变量。所以4 * 4 = 16个字节。]

Now I have tested the created file "Test.bin". The filesize is 22 bytes. But calculating what I have actually written in the file, the filesize should be 16 bytes. [int is 4 bytes each. 4 int variables. So 4 * 4 = 16 bytes.]

那么额外的 6个字节是多少? [22 - 16 = 6个字节]我用十六进制编辑器测试了该文件。这就是我发现的:

So what is the extra 6 bytes? [22 - 16 = 6 bytes] I have tested the file with a hex editor. That is what I have found:


ac ed 20 05 77 10 20 20 04 20 20 20 02 20 20 20 01 20 20 20 20 80

ac ed 20 05 77 10 20 20 04 20 20 20 02 20 20 20 01 20 20 20 20 80

我测试了更多不同代码的文件。我发现文件的前5个字节完全相同。第6个字节表示实际大小文件应该是什么。在这个例子中,十六进制是 10 ,这是十进制16 - 这是正确的。

I have tested more files with different codes. I have found that the first 5 bytes of the files are exactly same. The 6th byte represents the actual size what the file should be. In this example, it is 10 in hex, which is decimal 16 - that is correct.

现在,我的问题是如何在Java中避免这些字节?为什么即使是原始类型也能保存它们?我的真实应用程序仍然具有版本兼容性目的的显式标头信息出于任何目的,我不需要Java中的额外字节。虽然它是 6字节,但它只是浪费。

Now, my question is how to avoid these bytes in Java? Why are they saved even for primitive types? My real application still has explicit header info for version compatibility purpose. I do not need the extra bytes from Java for any purposes. Though it is 6 bytes, it is simply a waste.

推荐答案

来自 的ObjectOutputStream


原始数据(不包括可序列化字段和可外部化数据)将写入块数据记录中的ObjectOutputStream。块数据记录由标题和数据组成。块数据头由标记和标头后面的字节数组成。连续的原始数据写入被合并到一个块数据记录中。用于块数据记录的阻塞因子将是1024字节。每个块数据记录将填充最多1024个字节,或者在块数据模式终止时写入。调用ObjectOutputStream方法writeObject,defaultWriteObject和writeFields最初终止任何现有的块数据记录。

Primitive data, excluding serializable fields and externalizable data, is written to the ObjectOutputStream in block-data records. A block data record is composed of a header and data. The block data header consists of a marker and the number of bytes to follow the header. Consecutive primitive data writes are merged into one block-data record. The blocking factor used for a block-data record will be 1024 bytes. Each block-data record will be filled up to 1024 bytes, or be written whenever there is a termination of block-data mode. Calls to the ObjectOutputStream methods writeObject, defaultWriteObject and writeFields initially terminate any existing block-data record.

如果你只想写 byte s,使用 FileOutputStream

If you only want to write bytes, use a FileOutputStream.

这篇关于在Java中编写文件时如何避免额外的头字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆