gpt4 book ai didi

java - RandomAccessFile 读取西里尔文 UTF-8 java

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

伙伴们!

我在使用 RandomAccessFile 读取文件西里尔文本时遇到问题。

这是一个简单的程序,它使用以下格式将信息写入特定文件(西里尔字母):

keyLength、valueLength、key、value

然后程序尝试读取此信息,但我的输出不正确:

writing success
keyLength = 10, valueLength = 4
read: килло, гр

UPD预期输出:

writing success
keyLength = 10, valueLength = 4
read: киллограмм, сала

有什么问题吗? (除了我脑子小的问题)

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.io.IOException;

public class Main {

public static void main(String[] args) {
String fileName = "file.db";
RandomAccessFile outputFile = null;

try {
outputFile = new RandomAccessFile(fileName, "rw");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
}

String key = "киллограмм";
String value = "сала";

try {
outputFile.writeInt(key.length());
outputFile.writeInt(value.length());

outputFile.write(key.getBytes("UTF-8"));
outputFile.write(value.getBytes("UTF-8"));
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}

System.out.println("writing success");

RandomAccessFile inputFile = null;

try {
inputFile = new RandomAccessFile(fileName, "r");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
}

int keyLength = 0, valueLength = 0;

try {
keyLength = inputFile.readInt();
valueLength = inputFile.readInt();
} catch (IOException e) {
System.err.println(e.getMessage());
}

System.out.println("keyLength = " + keyLength + ", valueLength = " + valueLength);
if (keyLength <= 0 || valueLength <= 0) {
System.err.println("key or value length is negative");
System.exit(1);
}

byte[] keyBytes = null, valueBytes = null;

try {
keyBytes = new byte[keyLength];
valueBytes = new byte[valueLength];
} catch (OutOfMemoryError e) {
System.err.println(e.getMessage());
System.exit(1);
}

try {
inputFile.read(keyBytes);
inputFile.read(valueBytes);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}

try {
System.out.println("read: " + new String(keyBytes, "UTF-8") + ", " + new String(valueBytes, "UTF-8"));
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}

}
}

最佳答案

问题是这样的

outputFile.writeInt(key.length());

String#length()

Returns the length of this string. The length is equal to the number of Unicode code units in the string.

在本例中,它返回值 10,这不是表示此字符串所需的字节数。

你想要的是

key.getBytes("UTF-8").length

用作

byte[] keyBytes = key.getBytes("UTF-8");
outputFile.writeInt(keyBytes.length);

相同。

关于java - RandomAccessFile 读取西里尔文 UTF-8 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624951/

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