RandomAccessFile 乱码问题 hpunix

RandomAccessFile 乱码问题,纠结了很久,终于有点眉目了


从日志文件读取下面的汉字时遇到乱码,具体的java写法如下:(考虑用RandomAccessFile 是因为能够知道文件的位置)

String name ="D:/workspace/lcunp50_cmhn/output/log/logparse/getpw_client_parse.log";
String source = "iso8859-1";
String des = "gb2312";
File file = new File(name);
RandomAccessFile rf = null;
String str=null;
System.out.println("当前JRE:" + System.getProperty("java.version"));
System.out.println("当前JVM的默认字符集:" + Charset.defaultCharset());
System.out.println("当前JVM的默认字符集:" + System.getProperty("file.encoding"));

// Set set = Charset.availableCharsets().keySet();
// Iterator ite = set.iterator();
// while(ite.hasNext()){
// System.out.println(ite.next());
// }


try {
rf = new RandomAccessFile(file, "r");
// 设置起始位置
rf.seek(0);


int count = 0;

str = rf.readLine();
System.out.println("1---->"+str);
//字符集转换
str=new String(str.getBytes(source),des);
System.out.println("2---->"+str);


} catch (Exception e) {
System.out.println(e);
}finally{
try {
if(rf!=null) rf.close();
rf=null;
} catch (Exception e) {
}
}


日志汉字:“敬的用户您好!获取密码失败,详情请咨询10086。

主机环境:

HP-UX UNCS-PS- B.11.31 U ia64 2778565412 不限用户数的许可证

jdk:

$ java -version
java version "1.5.0.19"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0.19-_18_dec_2009_00_08)
Java HotSpot(TM) Server VM (build 1.5.0.19 jinteg:12.17.09-23:38 IA64, mixed mode)

上面检查程序答应结果:System.out.println("1---->"+str);

  ^d1^a5^d1^c4^d1^>0^d1^~0^d1^~1^d1^>6^d1^a1^d1^>5^d1^~7^d1^|7^d1^>6^d1^d4^d1^~6^d1^>5^d1^|3^d1^|1^d1^~7^d1^c5^d1^`0^d1^|1^d1^>5
^d1^b0^d1^>4^d1^b9^d1^`2^d1^|7^d1^}6^d1^b0^d1^|3^d1^}2^d1^`7^d1^b8^d1^>9^d1^b7^d1^>9^d1^b9^d1^a5^d1^`1^d1^`9^d1^}510086^d1^|1^d1^|3


换jdk后 

$ java -version
java version "1.5.0.11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0.11-_07_nov_2007_10_59)
Java HotSpot(TM) Server VM (build 1.5.0.11 jinteg:11.07.07-16:23 IA64, mixed mode)


解析的结果:System.out.println("1---->"+str);

×^a0^j8^a0^f6^a0^e7^a0^e8^a0^g2^a0^h7^a0^g1^a0^f3§^a0^g2ú^a0^f2^a0^g1^a0^d5^a0^d3^a0^g1^a0^i5^a0^g0^a0^j5^a0^h6^a0^h5·^a0^d4
^a0^g9^a0^h1^a0^e8^a0^f5^a0^g2ú^a0^e8^a0^g2^a0^g8^a0^i0^a0^f3ú^a0^d5^a0^e1^a0^g5^a0^j5^a0^e5é^a0^g8^a0^h9^a0^d3^a0^d5


因为没有下载源码,调查str = rf.readLine();的处理方法是否有所不同。个人觉得这块应该2个版本有差异。(待有时间查阅源码进行验证)


之前调整了日志文件的字符编码,主机环境的字符编码都没有解决。


你可能感兴趣的:(乱码问题,ja)