-1

For example the file that I needed is found at this filepath and it will be passed as an argument:

"C:\Users\user.name\docs\jap\あああいいいうううえええおおおダウンロード\filename.txt"

I used this code to decode the characters:

String new_path = new String(args[0].getBytes("Shift_JIS"), StandardCharsets.UTF_8);
System.out.println(new_path);

However, the output is:

C:\Users\user.name\docs\jap\あああい�?�?�?�?�?えええおおお�?ウンロード\filename.txt

Some of the characters have not been decoded properly. I already changed the text encoding and encoding of the console to UTF-8 but it still didn't work.

But if I would just print it regularly, it displays just fine.

System.out.println("C:\\Users\\user.name\\docs\\jap\\あああいいいうううえええおおおダウンロード\\filename.txt");

which displays:

C:\Users\user.name\docs\jap\あああいいいうううえええおおおダウンロード\filename.txt

Please tell me how to read the other characters, it really be a great help. Thanks!

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
waanderer
  • 157
  • 1
  • 1
  • 11
  • Possible duplicate of [how to use chinese and japanese character as string in java?](https://stackoverflow.com/questions/4067628/how-to-use-chinese-and-japanese-character-as-string-in-java) – Echoinacup Jan 18 '19 at 10:04
  • try using this `String path = "C:\\Users\\user.name\\docs\\jap\\あああいいいうううえええおおおダウンロード\filename.txt"; String new_path = new String(path.getBytes(), StandardCharsets.UTF_8);` – Doctor Who Jan 18 '19 at 10:07
  • 5
    You are telling String.getBytes to give you the bytes in 'Shift_JIS' encoding and then you are telling the String constructor that the byte encoding is UTF-8 - this is clearly wrong. You should just be able to use the args[0] string directly without any conversion. – greg-449 Jan 18 '19 at 10:14
  • @suneel it should be passed as an argument, thus using args[0] – waanderer Jan 18 '19 at 10:19
  • @greg-449 I did try just printing the args[0] string directly, however, the output is: C:\Users\user.name\docs\jap\縺ゅ≠縺ゅ>縺�縺�縺�縺�縺�縺医∴縺医♀縺翫♀繝�繧ヲ繝ウ繝ュ繝シ繝噂filename.txt – waanderer Jan 18 '19 at 10:19
  • Then something has mangled the string before your program gets it. You will have to investigate in detail what is happening to the string. Testing here running a program in Eclipse works fine with just args[0]. – greg-449 Jan 18 '19 at 10:30
  • `String new_path = args[0];` should suffice, the conversion of the platform encoding to java String (always Unicode) already is done. `System.out.println` will convert String's Unicode to the platform's encoding. – Joop Eggen Jan 18 '19 at 10:33
  • SideNote: name your variables in camel case (first letter lower case, and every following word capitalised: `new_path` -> `newPath`) – Lino Jan 18 '19 at 10:34

1 Answers1

0
public static void main(String[] args) throws UnsupportedEncodingException {
    // it is your code
    String newPath = new String(args[0].getBytes("Shift_JIS"), StandardCharsets.UTF_8);
    System.out.println(newPath);
    // instead of your code
    newPath = args[0];
    System.out.println(newPath);
}

maybe, you can show "あああいいいうううえええおおおダウンロード".

if you create the String object with a byte array and corresponding charset, you can convert it to any charset for it.

kiho.kim
  • 1
  • 2
  • I tried your code, however it returned: C:\Users\user.name\docs\jap\縺ゅ≠縺ゅ>縺�縺�縺�縺�縺�縺医∴縺医♀縺翫♀繝�繧ヲ繝ウ繝ュ繝シ繝噂filename.txt – waanderer Jan 18 '19 at 10:25
  • I use KOREAN windows. but I execute my code, E:\temp\縺ゅ≠縺ゅ>java UTF8PathTest あああいいいうううえええおおおダウンロード ???????????????????????????????_?E??????h あああいいいうううえええおおおダウンロ?ド – kiho.kim Jan 18 '19 at 10:31
  • Did you use Eclipse? if so what encoding did you use? – waanderer Jan 21 '19 at 00:59