简体   繁体   English

由于Java中的特殊字符而无法打开文件

[英]Cannot open File because of special characters in Java

My problem is that I cannot open a file with special characters in Java. 我的问题是我无法在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 我有一个诸如solution/Testàtest.pdf类的文件,如果我在Linux中使用java -jar program.jar运行Java程序,

I recieve that error message: 我收到该错误信息:

java.io.FileNotFoundException java.io.FileNotFoundException

because he looks the path as solution/Testtest.pdf and not like supposed as solution/Testàtest.pdf 因为他认为路径为solution/Testtest.pdf而不是假定为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. 您可能在Java或操作系统中的文件名中有错字(如@Berger在注释中指出的),或者您遇到了字符串编码问题。

I run linux and have my Java-source-file encoding on UTF-8. 我运行linux,并在UTF-8上使用Java源文件编码。

I have a file: /home/raudenaerde/ħ.txt <-- notice the nice character :) 我有一个文件:/home/raudenaerde/ħ.txt<-注意漂亮的字符:)

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: ) 如果不确定Java源文件的编码,则可以始终使用unicode-escape(在这种情况下为

To print your locale on linux, run the command: 要在Linux上打印您的语言环境,请运行以下命令:

locale

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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