Java中用数据流读记事本中的数字显示不正常

来源:百度知道 编辑:UC知道 时间:2024/06/07 16:34:25
我用java编写程序读取.txt文件中的数字,但总是显示不正常,比如记事本中是数字1,读到JTextArea中就成了3276832,这是什么原因?(记事本是unicode编码,不然不能显示汉字)
真是不好意思忘说了,规定用数据流或对象流

你一定是从字节流转成字符流的时候没有写字符集,所以才会成了乱码。
像下面那样指定字符集为"unicode"就没有问题了。
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\a.txt"), "unicode"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

给你写个方法,你用下吧。
public void readFile(String filename){
BufferedReader in = null;
String s = null;
try {
in = new BufferedReader(new FileReader(filename));
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
in.close();
} catch (Exception e) {
System.out.println("打开失败!");
}
}

串行化?
那个怎么能用来