Mac OS FileNotFound上的Java,如果路径包含非拉丁字符 [英] Java on Mac OS FileNotFound if path contatins non latin characters
问题描述
每个人都好消息。
我在Mac OS上发现java的问题。
I found intersted problem with java on my Mac OS.
我把名称从非明字符到文件夹测试的文件,完整路径为 / Users / dnelepov / Downloads / test /тестизображение.png
I put file with name from nonlatin characters to folder test, full path is /Users/dnelepov/Downloads/test/тест изображение.png
正如您所见文件存在!
接下来我写了一些代码来查看文件夹 / Users / dnelepov / Downloads / test / 上的所有文件打开文件进行阅读(例如):
Next i wrote some code to view all files on folder /Users/dnelepov/Downloads/test/ and open file for read (for example):
File dirF = new File("/Users/dnelepov/Downloads/test/");
File[] files = dirF.listFiles();
for (File oneF : files) {
System.out.println("FILE:" + oneF);
if (oneF.exists()){
System.out.println("EXISTS:"+oneF);
}
}
但接下来我得到了这个:
But next i got this:
正如您所见,EXSIS行:从未打印过,但已创建文件!
As you can see line EXSIS: never printed, but file founded!
此外,printind系统属性获得:
Also printind system properties get:
file.encoding = UTF-8
ps
在带有西里尔字符的文件路径的Java java.io.filenotfoundexception - 但我认为这个问题不仅适用于西里尔字符 - 而是适用于所有非拉丁语言。
I ask similar type question before Java java.io.filenotfoundexception for file path with cyrillic characters - but i think this problem not for only Cyrillic characters - but for all non latin languages.
推荐答案
在Java 7中,您可以使用java.nio.file API,它可以正常使用西里尔语路径:
In Java 7 you can use java.nio.file API, it works properly with cyrillic paths:
Path path = Paths.get("/Users/dnelepov/Downloads/test/");
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path oneF, BasicFileAttributes attrs) throws IOException {
System.out.println("FILE:" + oneF);
if (Files.exists(oneF)) {
System.out.println("EXISTS:" + oneF);
}
return FileVisitResult.CONTINUE;
}
});
这篇关于Mac OS FileNotFound上的Java,如果路径包含非拉丁字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!