在java中apache提供了一个工具类BOMStream,在获取文件流时,将获取到的文件流转化成为BOM流:
InputStreamReader is = new InputStreamReader (new BOMInputStream (fileInputStream), "utf8");
这种方法只能过滤掉UTF-8/UTF-16LE/UTF-16BE三种编码格式的文件,当文件是其他格式时,就会失效。只能使用另外一个工具了:UnicodeStream和UnicodeReader:
FileInputStream fis = new FileInputStream(file); UnicodeReader ur = new UnicodeReader(fis, "utf-8"); BufferedReader br = new BufferedReader(ur);