您的位置 首页 java

java读取txt乱码解决方法

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乱码解决方法

文章地址:https://www.zhihuclub.com/16474.shtml

关于作者: 智云科技

热门文章

网站地图