您的位置 首页 技术

java读取txt中文乱码

java读取txt文本中如含有中文,可能会出现乱码,解决方案是: 1、要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8; 2、利用…

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知识请关注java基础教程。

以上就是java读取txt中文乱码的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/14792.html

为您推荐

返回顶部