简体   繁体   中英

Why can't the program read UTF-8 characters using Scanner?

I initialized the Scanner class object in this manner:

Scanner scanner = new Scanner(new File("data.txt"),"utf-8");  

When i try to read a file with chars like ç or é, scanner.hasNextLine() returns false, scanner don't read nothing.

I tried to use "iso-8859-1". And file reading was successful. But file is UTF-8 file and chars like 'ç' are displayed as "ç".

Please help me solve the problem and make the program properly read and display UTF-8 characters.

specify encoding while writing the UTF-8 encoded text

new String(scanner.next().getBytes(), Charset.forName("UTF-8"))

To get the complete line, specify delimiter in Scanner

scanner.useDelimiter("\n");

Use:

new String(scanner.next().getBytes("UTF-8"), Charset.forName("UTF-8"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM