缓冲流读取文件时出现中文乱码

        Scanner scanner=new Scanner(System.in);
        File file=new File("C:\\Users\\Administrator\\Desktop","t1.txt");
        String n=null;
        char[] b=new char[3];
        try {
            FileReader in=new FileReader(file);
            BufferedReader br=new BufferedReader(in);
            while((n=br.readLine())!=null){
                System.out.println(n);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

缓冲流读取文件时出现中文乱码,网上搜到可能是由于电脑本身的txt文件的编码是ANSI,而IDEA的默认编码是UTF-8。
把txt文件的编码格式改为UTF-8之后就没有乱码了

用Notepad++打开文件

点击 编码->转为UTF-8编码格式

参考:https://blog.csdn.net/dzxdai/article/details/104782809/

posted @ 2021-08-24 09:05  0牛牛牛  阅读(205)  评论(0)    收藏  举报