public static void main(String[] args) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("e:/ok.txt","r");
randomAccessFile.seek(75);
String str;
while ((str = randomAccessFile.readLine()) != null) {
// 解决乱码 , 第二个参数是文档编码
str = new String(str.getBytes("8859_1"),"UTF-8");
System.out.println(str);
}
randomAccessFile.close();
}
RandomAccessFile的readLine(),中文乱码情况
该代码示例展示了如何使用Java的RandomAccessFile类从指定位置读取文件,并进行编码转换以解决乱码问题。它首先打开一个以r模式读取的文件,然后定位到特定位置,接着逐行读取并用8859_1转成UTF-8编码,确保输出正确。
摘要由CSDN通过智能技术生成