Home > Java > javaTutorial > body text

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

高洛峰
Release: 2017-01-20 16:51:08
Original
1837 people have browsed it

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!