java读取txt文本发生乱码的解决方法

发布时间:2020-06-25 17:35:58 作者:Leah
来源:亿速云 阅读:158

本篇文章为大家展示了java读取txt文本发生乱码的解决方法,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

解决方案是:

1、要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8;

2、利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")将文本再次设置为utf-8

InputStreamReader isr;   
    try {   
        isr = new InputStreamReader(new FileInputStream(fileUrl), "utf-8");   
         BufferedReader read = new BufferedReader(isr);   
           String s=null;   
           List<String> list = new ArrayList<String>();   
           while((s=read.readLine())!=null)   
           {   
               //System.out.println(s);   
               if(s.trim().length()>1){   
               list.add(s.trim());   
               }   
           }   
              
           System.out.println("OK!");   
    } catch (UnsupportedEncodingException e) {   
               e.printStackTrace();   
    } catch (FileNotFoundException e) {   
               e.printStackTrace();   
    } catch (IOException e) {   
               e.printStackTrace();   
    }

上述内容就是java读取txt文本发生乱码的解决方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. java txt文件写入时发生中文乱码怎么办
  2. Java读取文本文件发生乱码的解决方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java txt ava

上一篇:use options instead of chrome_

下一篇:python Web开发的参考书

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》