java fileinputstream中文乱码

2021-09-18  本文已影响0人  Mracale
public static void main(String[] args) throws IOException {
        // 创建File对象
        File file = new File("C:\\Users\\Desktop\\test.txt");
        InputStream fis=null;
        try {
            fis = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); //最后的"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();
                }
            }
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读