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 ();