Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将对象写入文件?_Java_C - Fatal编程技术网

如何在java中将对象写入文件?

如何在java中将对象写入文件?,java,c,Java,C,我想把下面的c代码转换成java。我不知怎么做了,但在写入文件后,我比较了《超越比较》中的两个内容,发现有很多不同之处。 请建议我如何用java编写与c相同的代码 C代码: const struct bin_header bin_header = { .magic = "VVN", .header_size = 0x10, .version = (0x01 << 24) | 0x010000, .core = (0x02 << 24) | 0x000501, };

我想把下面的c代码转换成java。我不知怎么做了,但在写入文件后,我比较了《超越比较》中的两个内容,发现有很多不同之处。 请建议我如何用java编写与c相同的代码

C代码:

const struct bin_header bin_header = {
 .magic = "VVN",
 .header_size = 0x10,
 .version = (0x01 << 24) | 0x010000,
 .core = (0x02 << 24) | 0x000501,
};
FILE* ofp = fopen(outfilename, "wb");
fwrite(&bin_header, sizeof(bin_header), 1, ofp)

我找到了一种方法,可以将对象成员写入具有endianness的文件。 我用了ByteBuffer

   int write(FileOutputStream fout) throws IOException {
            int bytes;
            FileChannel fChan = fout.getChannel();
        ByteBuffer str = ByteBuffer.wrap(magic.getBytes());
        bytes = fChan.write(str);

        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.putInt(header_size);
        buf.rewind();
        bytes  += fChan.write(buf);
        buf.clear();
        ...

        return bytes;
   }

int read(FileInputStream fIn)  throws IOException {
    int bytes = 0;
    FileChannel fChan = fIn.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.order(ByteOrder.LITTLE_ENDIAN);

    bytes = fChan.read(buf);
    buf.rewind();
    magic = new String(buf.array());
    buf.clear();

    ...

    return bytes;
}

我想你是在寻找序列化,谷歌就是这样!您不能为此使用
ObjectOutputStream
;您还必须知道字符串使用了什么编码。使用
DataOutputStream
。另外,您知道您的C代码依赖于endianness,对吗?@fge您能详细说明他为什么不能使用ObjectOutputStream吗?我正在测试一个解决方案,它可以正常工作well@Aaron因为ObjectOutputStream将包含一个Java特定的头(如果未在类中指定,
serialVersionUID
的值,或者是一个默认值;而OP尝试直接写入原始信息(以依赖于对齐的方式引导,meh)。当然还有字符串问题:序列化字符串在很大程度上取决于您选择使用的编码。当然有{read,write}UTF(),但不知道如何在C中读取它。
writeByVVN() {
    bin_header bin_header = new bin_header();
    Fout = new FileOutputStream(outFile);
    ObjectOutputStream oos = new ObjectOutputStream(Fout);
    oos.writeObject(bin_header);
}
   int write(FileOutputStream fout) throws IOException {
            int bytes;
            FileChannel fChan = fout.getChannel();
        ByteBuffer str = ByteBuffer.wrap(magic.getBytes());
        bytes = fChan.write(str);

        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.putInt(header_size);
        buf.rewind();
        bytes  += fChan.write(buf);
        buf.clear();
        ...

        return bytes;
   }

int read(FileInputStream fIn)  throws IOException {
    int bytes = 0;
    FileChannel fChan = fIn.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.order(ByteOrder.LITTLE_ENDIAN);

    bytes = fChan.read(buf);
    buf.rewind();
    magic = new String(buf.array());
    buf.clear();

    ...

    return bytes;
}