How do I check if a file exists in Java when the file path contains chinese words in Linux?

Viewed 133

I run the code well when the file path is all English or Chines,but When my file path has both Chinese and English, the code result is false.In my window system,I try to encode the file path,and it runs well.But when I try to deploy it in the Linux system,It return false,the file path is:/home/file/GPRS核心.zip. How to fix my code?

//the first version
new File(fileName).exists()
//the second version
new File(new String(fullFileName.
            getBytes(StandardCharsets.ISO_8859_1))).exists()
//the third version:return true in window and return false in linux
new File(new String(fullFileName.
            getBytes(StandardCharsets.UTF_8))).exists()

The linux encode is here:

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
1 Answers

I do not have a chinese system so cannot recreate that behaviour.

However I know if you run Windows configured for chinese, your filesystem should support unicode filenames. Java on it's own supports Unicode as it runs on UFT16. The problem you describe should not occur at all.

Therefore I strongly believe you introduced the problem by placing ISO_8859_1 encoding inbetween. Remove that stuff and you should be good.

In general do not use getBytes() - a character is not a byte.

Related