当前位置: 代码迷 >> J2SE >> 啊java编码有关问题
  详细解决方案

啊java编码有关问题

热度:28   发布时间:2016-04-23 21:39:46.0
求助啊,,,java编码问题
读取文件CharsetDemo_003.txt内容,保存格式是UTF-8,内容为
欢迎光临welcome

为什么输出结果为
?欢迎光临welcome

import java.io.*;

public class CharsetDemo_003 {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
try {
String currentCharset = "utf-8";
File file=new File("CharsetDemo_003.txt");   
int size = (int) file.length();
byte[] b = new byte[size];

in = new FileInputStream(file);
in.read(b);

System.out.println("1 --以下显示明文: ");
String decoding = new String(b, currentCharset); // 字符解码
System.out.println(decoding);

System.out.println("\n\n2 --以下显示乱码:");
currentCharset = "gbk";
decoding = new String(b, currentCharset); // 字符解码
System.out.println(decoding);

} finally {
if (in != null)
in.close();
}
}
}
java

------解决方案--------------------
用Ultraedit或Notepad++等编辑器,把文件编码保存为UTF-8 without BOM再试试。
  相关解决方案