Home>Article>Java> How to read txt text line by line in java to solve Chinese garbled characters

How to read txt text line by line in java to solve Chinese garbled characters

高洛峰
高洛峰 Original
2017-01-20 16:51:08 1826browse

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 list = new ArrayList(); 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn