gpt4 book ai didi

Java - RandomAccessFile() 格式问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:15 25 4
gpt4 key购买 nike

我的问题是,当我从文本文件获取数据时,它似乎采用了错误的编码或其他内容。抱歉,我不知道如何解决这个问题。提前致谢。这是代码。

private static String readLogFile(String path, File f) throws IOException {
if (f.exists()){
String data;
try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
char ch = raf.readChar();
raf.seek(f.length());
String dataCh = String.valueOf(ch);
data = dataCh.toString();
System.out.println(data);
}
return data;
}

else{
String data = "";
return data;
}
}

最佳答案

来自 Java 文档

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

注意,“读取了两个字节”

因为 Java 支持 Unicode 字符,所以流会自动假设从流中读取字符需要生成 Unicode 字符。

尝试使用char ch = (char)raf.readByte();代替

关于Java - RandomAccessFile() 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12170118/

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