java读取txt文本中如含有中文,可能会出现乱码,解决方案是:
1.要统一编码,java工程的编码,txt文本编码,java工程中的java文本编码都统一为utf-8;
2.利用 InputStreamReader(new FileInputStream(fileUrl), "utf-8")将文本再次设置为utf-8
3.具体代码如下
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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
更多java 逐行读取txt文本如何解决中文乱码相关文章请关注PHP中文网!