使用随机访问文件类解决readLine乱码问题
文件读取之所以会乱码,是因为在RandomAccessFile下使用自述文件方法时,编码会自动变为ISO-8859-1。 因此,可以如下进行
randomaccessfilefl=newrandomaccessfile (文件,“rw”);
wile(fl.getfilepointer ) )。
system.out.println (new string (fl.readline ).GetBytes )“iso-8859-1”、“gb2312”)//gb2312是您的文本编码
}
使用随机访问文件向数据库写入中文时,
*使用*write(string.getbytes )、write utf (string )、writechars(string )和write utf (write utf ) string )时,都会出现乱码。 1、String.getBytes ()根据当前系统的缺省编码方法获取字符串的Bytes,randomaccessfile.write ) )正确写入此byte数组。 写入实际上是Windows平台的本机代码,因此文件也可以正确读取。
2、random access file.write bytes (字符串)是字符串中的每个字符(当然是用unicode编码的),去掉前8位,写入文件。
3、random access file.write chars (字符串)将字符串中的各个字符按照unicode的编码以Big-endian方式写入文件。 在Windows平台上,缺省文件编码为Little-endian,因此在平板电脑上打开时会出现乱码,但在浏览器中打开此文件(testWriteChars.dat )以使用uuse编码
4、random access file.write utf (string )首先写00 03,然后写三个实际字节,写“中”UTF-8代码(E4 B8 AD )
通过以上分析,使用RandomAccessFile写中文时,建议使用random access file.write (string.getbytes ) )方式。 为了保险起见,还可以指定运行平台的默认本机代码编码。 例如
参考链接:https://www.cn blogs.com/1130136248 wlxk/articles/5109331.html