简体   繁体   中英

Cannot open File because of special characters in Java

My problem is that I cannot open a file with special characters in Java.

For example,

I have a file like solution/Testàtest.pdf and if I run my java program in Linux with java -jar program.jar

I recieve that error message:

java.io.FileNotFoundException

because he looks the path as solution/Testtest.pdf and not like supposed as solution/Testàtest.pdf

My code to open a File is:

File d1 = new File("solution/Testàtest.pdf");

So, have everyone an idea how to resolve this problem ?

You probably have either a typo in your filename in Java or on the OS (as @Berger pointed out in the comments), or you have a String-encoding issue.

I run linux and have my Java-source-file encoding on UTF-8.

I have a file: /home/raudenaerde/ħ.txt <-- notice the nice character :)

public static void main(String[] args) {
    File f = new File("/home/raudenaerde/ħ.txt");

    System.out.println(f.exists());
}

When I run this:

true

If you are not sure about the java source file encoding, you could always use the unicode-escape ( in this case: )

To print your locale on linux, run the command:

locale

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