Java带有西里尔字符的文件路径的java.io.filenotfoundexception [英] Java java.io.filenotfoundexception for file path with cyrillic characters

查看:111
本文介绍了Java带有西里尔字符的文件路径的java.io.filenotfoundexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其名称不仅包含纯ASCII字符集的字符,而且还包含非ASCII字符集的字符.就我而言,它包含西里尔字母.

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.

这是我的代码的片段:

String fileName = "/Users/dnelepov/Downloads/тест изображение.png";
File sendFile = new File(fileName);
if (sendFile.exists()) {
    // Some code
}

sendFile.exists if块中的代码未执行.

The code in sendFile.exists if block is not being executed.

为什么无法识别文件?

我的系统配置 区域设置

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)

更新

我发现此错误是来自Oracle的JDK.

I found that this error is on JDK from Oracle.

我在Eclipse上创建了项目,并找到了文件.我检查了项目属性并找到了Mac OS 6 JDK.

I created project on Eclipse, and file was found. I checked project properties and found Mac OS 6 JDK.

然后我将其更改为JDK 7,并且再次找不到文件.

Then I change it to JDK 7 and file was not Found again.

我的问题是我需要将JDK 7与JavaFX一起使用.不是Mac OS版本.所以我的问题仍然存在.

My problem is that I need to use JDK 7 with JavaFX. Not Mac OS version. So my problem still exists.

我制作了一个显示此错误的视频 更新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

推荐答案

只是为了踢球,这种技巧可能会起作用:

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
          ...
    }

我不知道它是否会为您带来任何不同的结果,特别是因为它可能只是文件名的奇怪编码问题,但这可以帮助您了解情况.如果代码没有执行,请在charArray的int(ascii vals)上打印目录中的所有文件名-找到您要查找的文件,并查看chars的编码方式以及编码原因不相等.

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.

这篇关于Java带有西里尔字符的文件路径的java.io.filenotfoundexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆