日期:2014-05-20  浏览次数:20632 次

请问 文本中含有汉字,如何读取?
请问   文本中含有汉字,如何读取?
比如txt文件!

请给出简单代码,谢谢!

------解决方案--------------------
不用管中文英文,都可以把它看作二进制流,用DataInputStream读就可以了.
例如DataInputStream input=new DataInputStream(new FileInputStream( "test.txt "));
再用DataInputStream的方法就可以了
------解决方案--------------------
用字符流
------解决方案--------------------
import java.io.*;

public class Test2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// System.setProperty( "file.encoding ", "utf-8 ");

BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream( "d:\\huhu1.txt "), "utf-8 "));
// DataInputStream f2 = new DataInputStream( new
// FileInputStream( "aa.txt ") );

String s = null;
while ((s = f.readLine()) != null) {

System.out.println(new String(s.getBytes(), "gbk "));

}
}

}


指定读入的编码就可以了

不然会按照OS 默认的编码