首先需要說明讀文件的兩種最常見的方式:
第一種是:getResourceAsStream(fileName)
第二種是:
String filePath = ReadAndWriteHandler.class.getClassLoader().getResource(FILE_NAME).getFile();
filePath = java.net.URLDecoder.decode(filePath, ENCODING);
File file = new File(filePath);
不管是哪一種,一般都需要傳入 new InputStreamReader(xxx) 中
此時,如果不給 new InputStreamReader(xxx) 加編碼參數,會采用默認的本機默認的編碼(如Win的GBK)讀入。
如果,文件格式是utf-8,此時,就會出現亂碼,因此,對於utf-8編碼的文件,我們需要加上編碼參數:
new InputStreamReader(xxx, "utf-8");