【发布时间】:2025-12-04 07:00:01
【问题描述】:
当我尝试从文件中读取文本时,使用 LineNumberReader 时底层文本无法正确显示
试图从文件中读取的文本 -
¥ · £ · € · $ · ¢ · ₡ · ₢ · ₣ · ₤ · ₥ · ₦ · ₧ · ₨ · ₩ · ₪ · ₫ · ₭ · ₮ · ₯ · ₹
示例代码--
FileInputStream fis = null;
try {
fis = new FileInputStream("C:\\Users\\JavaUser4\\Desktop\\checkImort.txt");
InputStreamReader streamReader = new InputStreamReader(fis, "UTF-8");
LineNumberReader reader = new LineNumberReader(streamReader);
String sLine = reader.readLine();
System.out.println(sLine);
} catch (Exception ex) {
} finally {
try {
fis.close();
} catch (IOException ex) {
}
}
输出 -
? ? ? ? ? ? $ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
请使用LineNumberReader 帮助正确阅读此文本。我更喜欢留在LineNumberReader,因为我使用的是RandomAccessFile,根据我的要求,这是一个完美的解决方案
- 打开一个包含 UTF-8 编码文本的文件。
- 设置我们需要开始读取文件的行号。
- 从文本文件中读取 25 行。
- 获取偏移的最后位置。
- 退出。
- 再次打开一个文件。
- 设置行号,我们需要从这里开始读取同一文件中接下来的 25 行。
- 从文本文件中读取 25 行。
- 获取最后一个偏移量。
- 等等。
缺点是 RandomAccessFile 不支持 UTF-8 编码,我搬到了 LineNumberReader 但这里也发生了同样的事情。请帮忙。
【问题讨论】: