If java reads txt text that contains Chinese, garbled characters may appear. The solution is:
1. To unify the encoding, the encoding of the java project, the txt text encoding, and the java text encoding in the java project are all unified. utf-8;
2. Use InputStreamReader(new FileInputStream(fileUrl), "utf-8") to set the text to utf-8 again
3. The specific code is as follows
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();
More java How to solve Chinese garbled text by reading txt text line by line. For related articles, please pay attention to the PHP Chinese website!