Warm tip: This article is reproduced from serverfault.com, please click

Read Japanese Character using Scanner

发布于 2013-02-20 07:19:14

Possible duplicate: How can I read Chinese characters correctly using Scanner in Java?

My input file name may have japanese characters and I am trying to read the file name using Scanner.

Scanner sc = new Scanner(System.in,"utf-8");
System.out.println("Encoding is :" + Charset.defaultCharset());

System.out.println("Enter the path:");
inputFilePath = sc.nextLine();

and if my input is for eg - 漢字

When I print the file name my output is

Encoding is :UTF-8
Input File Path:漢字

I also tried,

byte[] jis = sc.nextLine().getBytes("Shift_JIS");
System.out.println(new String(jis));

and My output was

Input File Path:??��??�\

How can I rectify this?

Questioner
Abi
Viewed
1
Jayamohan 2013-02-20 16:02:01

Eclipse is reading from the Console which is set by default as UTF-8. To read the data you need to change the console encoding to Japanese supported encoding.

In the Run Configuration, change the encoding to Japanese supported encoding and try again.

enter image description here