美文网首页
java fileinputstream中文乱码

java fileinputstream中文乱码

作者: Mracale | 来源:发表于2021-09-18 09:45 被阅读0次
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();
                }
            }
        }
    }

相关文章

网友评论

      本文标题:java fileinputstream中文乱码

      本文链接:https://www.haomeiwen.com/subject/hfmegltx.html