gpt4 book ai didi

java - RandomAccessFile 将 ETX M 添加到文件开头

转载 作者:行者123 更新时间:2023-12-02 07:36:20 25 4
gpt4 key购买 nike

这是在 CentOS 6.2 上。我正在写入一个文本文件,它在开头添加了 ETX M。 (ETX是角色的名字)

file.setLength(0);
file.seek(0);
file.writeUTF(somestring);

最佳答案

引用documentation对于RandomAccessile.writeUTF()

First, two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for each character.

如果您不想这样做,请手动将字符串转换为字节,并使用基本的 write() 方法写入这些字节(注意:writeBytes() 不是你想要的)。但是,您将需要某种方法来跟踪字符串的大小,以便再次读取它(除非您使用固定宽度字段)。

关于java - RandomAccessFile 将 ETX M 添加到文件开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com