java读取txt中文乱码
内容摘要
java读取txt文本中如含有中文,可能会出现乱码,解决方案是: 1、要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8; 2、利用 InputStreamReader(new Fi
文章正文
java读取txt文本中如含有中文,可能会出现乱码,解决方案是:
1、要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8;
2、利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")将文本再次设置为utf-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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基础教程。
代码注释
[!--zhushi--]