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

Source: Internet
Author: User

If the txt text read by java contains Chinese characters, garbled characters may occur. solution:
1. Unified coding, java engineering encoding, txt text encoding, and java text encoding in java engineering must all be 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: Copy codeThe 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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.