首先需要说明读文件的两种最常见的方式:

第一种是: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"); 

相关文章:

  • 2021-08-13
  • 2021-08-13
  • 2021-03-26
  • 2021-09-16
  • 2021-10-04
  • 2020-10-16
  • 2022-01-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2022-01-18
  • 2021-10-26
  • 2022-12-23
相关资源
相似解决方案