在java中apache提供了一個工具類BOMStream,在獲取文件流時,將獲取到的文件流轉化成爲BOM流:java
InputStreamReader is = new InputStreamReader (new BOMInputStream (fileInputStream), "utf8");
這種方法只能過濾掉UTF-8/UTF-16LE/UTF-16BE三種編碼格式的文件,當文件是其餘格式時,就會失效。只能使用另一個工具了:UnicodeStream和UnicodeReader:apache
FileInputStream fis = new FileInputStream(file); UnicodeReader ur = new UnicodeReader(fis, "utf-8"); BufferedReader br = new BufferedReader(ur);