java.io.File:使用无效的文件名编码访问文件 [英] java.io.File: accessing files with invalid filename encodings

查看:76
本文介绍了java.io.File:使用无效的文件名编码访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于java.io.File的构造函数采用java.lang.String作为参数,因此似乎不可能告诉它在访问文件系统层时期望使用哪种文件名编码.因此,当您通常使用UTF-8作为文件名编码,并且有一些文件名包含编码为ISO-8859-1的变音符号时,您基本上就是 ** .这是正确的吗?

Because the constructor of java.io.File takes a java.lang.String as argument, there is seemingly no possibility to tell it which filename encoding to expect when accessing the filesystem layer. So when you generally use UTF-8 as filename encoding and there is some filename containing an umlaut encoded as ISO-8859-1, you are basically **. Is this correct?

更新:由于看似没有人得到它,请自己尝试:创建新文件时,环境变量LC_ALL(在Linux上)确定文件名的编码.在源代码中做什么都没关系!

Update: because noone seemingly gets it, try it yourself: when creating a new file, the environment variable LC_ALL (on Linux) determines the encoding of the filename. It does not matter what you do inside your source code!

如果您想给出正确的答案,请说明您可以在JVM假定LC_ALL = en_US.UTF-8的情况下,使用正确的ISO-8859-1编码创建文件(使用常规Java手段).文件名应包含ö,ü或ä这样的字符.

If you want to give a correct answer, demonstrate that you can create a file (using regular Java means) with proper ISO-8859-1 encoding while your JVM assumes LC_ALL=en_US.UTF-8. The filename should contain a character like ö, ü, or ä.

顺便说一句:如果您将编码不适合LC_ALL的文件名放入maven的资源路径中,它将跳过它....

BTW: if you put filenames with encoding not appropriate to LC_ALL into maven's resource path, it will just skip it....

更新II.

解决此问题: https://github.com/jjYBdx4IL/filenameenc

即.使f.exists()语句变为真.

ie. make the f.exists() statement become true.

更新III.

解决方案是使用java.nio.*,在我的情况下,您必须将File.listFiles()替换为Files.newDirectoryStream().我已经在github上更新了示例.顺便说一句:maven似乎仍然使用旧的java.io API....mvn clean失败.

The solution is to use java.nio.*, in my case you had to replace File.listFiles() with Files.newDirectoryStream(). I have updated the example at github. BTW: maven seems to still use the old java.io API.... mvn clean fails.

推荐答案

解决方案是使用新的API和 file.encoding .演示:

The solution is to use the new API and file.encoding. Demonstration:

fge@alustriel:~/tmp/filenameenc$ echo $LC_ALL
en_US.UTF-8
fge@alustriel:~/tmp/filenameenc$ cat Test.java
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test
{

    public static void main(String[] args)
    {
        final String testString = "a/üöä";
        final Path path = Paths.get(testString);
        final File file = new File(testString);
        System.out.println("Files.exists(): " + Files.exists(path));
        System.out.println("File exists: " + file.exists());
    }
}
fge@alustriel:~/tmp/filenameenc$ install -D /dev/null a/üöä 
fge@alustriel:~/tmp/filenameenc$ java Test
Files.exists(): true
File exists: true
fge@alustriel:~/tmp/filenameenc$ java -Dfile.encoding=iso-8859-1 Test
Files.exists(): false
File exists: true
fge@alustriel:~/tmp/filenameenc$ 

减少使用 File 的理由!

这篇关于java.io.File:使用无效的文件名编码访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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