解决FileInputStream 读取文件中文乱码问题(转)

当Java中使用 FileInputStream 读取txt等文档时,中文会产生乱码,解决方法如下:

try {
            fis = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(fis,"GBK"); //最后的"GBK"根据文件属性而定,如果不行,改成"UTF-8"试试
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
posted @ 2017-09-25 19:35  木&子  阅读(21623)  评论(0编辑  收藏  举报