得到乱码,只能以 GBK 方式去读,但是用户提供的文件也可能为其他格式,就无法以 GBK 格式来读取了。
开始考虑是否能够有种办法判断某个文件的实际格式,但是最后也没找到这样的方法,似乎没有办法可以做到这一点。
因此最终只能用 UTF-8 来统一处理,而由用户自己负责将他的文件转为 UTF-8 格式。
在处理文件时读取方式如下:
FileInputStream fis = new FileInputStream("d:/xxx.txt");
BufferedReader reader = new BufferedReader( new InputStreamReader( fis, "UTF-8" ) );
String line = null;
// read all the lines one by one
while ( (line = reader.readLine()) != null )
{
.......
}