问题
I have a file whose name contains characters not only from the plain ASCII character set, but also from a non-ASCII character set. In my case it contains Cyrillic characters.
Here's a snippet of my code:
String fileName = "/Users/dnelepov/Downloads/тест изображение.png";
File sendFile = new File(fileName);
if (sendFile.exists()) {
// Some code
}
The code in sendFile.exists
if
block is not being executed.
Why isn't the file recognized?
My system configuration locale
LANG="ru_RU.UTF-8"
LC_COLLATE="ru_RU.UTF-8"
LC_CTYPE="ru_RU.UTF-8"
LC_MESSAGES="ru_RU.UTF-8"
LC_MONETARY="ru_RU.UTF-8"
LC_NUMERIC="ru_RU.UTF-8"
LC_TIME="ru_RU.UTF-8"
LC_ALL="ru_RU.UTF-8"
uname -a
Darwin Dmitrys-MacBook-Pro.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
java -version
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
UPDATE
I found that this error is on JDK from Oracle.
I created project on Eclipse, and file was found. I checked project properties and found Mac OS 6 JDK.
Then I change it to JDK 7 and file was not Found again.
My problem is that I need to use JDK 7 with JavaFX. Not Mac OS version. So my problem still exists.
I've made a video to show this error Video with error
UPDATE 2
Thanks to eumust for answer, this code works:
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;
}
});
https://stackoverflow.com/a/17481204/849961
回答1:
Just for kicks, this hack might work:
String fDir = "/Users/dnelepov/Downloads/";
char[] fileName = "тест изображение.png".toCharArray();
File root = new File(fDir);
File[] folder = root.listFiles();
for (File f : folder)
if (Array.equals(fileName, f.getName().toCharArray()) {
//code here
...
}
I don't know if it will yield any different results for you, especially since it may be just a weird encoding issue with the file name, but this could help shed some light on the situation. If the code doesn't execute, do a print on the int (ascii vals) of the charArray for all of the file names in the directory -- find the one you're looking for and see how the chars are encoded and why it's not equal.
回答2:
I had the same with non-ascii chars and this helped (updated):
String fileName = "file:///Users/dnelepov/Downloads/тест изображение.png";
URI uri = new URI(null, null, fileName, null);
System.out.println("TS:" + uri.getPath);
System.out.println("EX:" + new File(uri).exists());
回答3:
The following code prints true on OSX when I am using Java 7 b21 with OSX 10.8.4. Based on your Kernel version it looks like you are using 10.7.
import java.io.File;
public class file {
public static void main(String[] args) {
File file = new File("/Users/jhawk28/Developer/filetest/тест изображение.txt");
System.out.println(file.exists());
}
}
Based on your included project, this is the output on my machine:
java -jar TestCyrilic.jar
EX:true
It looks like it is a bug that was fixed in OSX 10.8.
回答4:
I have replaced cyrillic characters with unicode equivalents and it seems to work for me:
String fileName = "/Users/user1/тест \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435.txt";
Give it a shot
来源:https://stackoverflow.com/questions/16968299/java-java-io-filenotfoundexception-for-file-path-with-cyrillic-characters