用windows的notepad.exe新建一个UTF-8编码的文件。然后用Java的FileInputStream读出来转换成UTF-8的字符第一个字符会是乱码。不知道为什么,有什么办法解决,先谢了。
如果各位有空,可能帮我测试一下。不知道是我的问题还是别的原因!
文件内容:<tr><td>a我爱你们</td></tr>
读取文件:
public class Test {
public static void main(String[] args) {
new Test().testFile();
}
public void testFile() {
File dir = new File("C:/a.txt");
try {
FileInputStream fi = new FileInputStream(dir);
byte[] bt = new byte[(int)dir.length()];
fi.read(bt);
System.out.println(new String(bt,"UTF-8"));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
[该贴被cnng007于2007年08月17日 22:07修改过]