IOException – 详细消息全是问号

huangapple go评论78阅读模式
英文:

IOException - detail message all question marks

问题

我想使用路径createNewFile,但是我收到了一个IOException。问题是,无法解释详细的消息,我只能看到一堆问号。

我原本使用的是安装了中文语言包的西班牙语Windows 10系统。Java语言已设置为en,文件编码为UTF-8

java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

为什么?只有这个异常无法阅读。

编辑:尝试将语言定义为zh,但不起作用。

使用此主类进行复现:

public class PromotionTargetFileHandlerMain {
    public static final String uploadingDir = String.join(File.separator,
            System.getProperty("user.dir"), "targets_csv");
    private static final File destDir = new File(uploadingDir);

    public static void main(String[] args) {
        createFileDestination("target.csv");
    }

    public static void createFileDestination(String filename) {
        if (!destDir.exists()) {
            try {
                Files.createDirectory(Path.of(uploadingDir));
            } catch (FileAlreadyExistsException e) {
                log.trace("File dir already exists: {}", uploadingDir);
            } catch (IOException e) {
                log.error("Cannot create temp file dir {}", uploadingDir, e);
                throw new RuntimeException(e);
            }
        }
        String saveLocation = String.join(File.separator,
                uploadingDir, filename
        );
        File saveFile = new File(saveLocation);
        if (saveFile.exists()) saveFile.delete();
        try {
            saveFile.createNewFile();   // <--------------- 这里是IOException的地方
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
英文:

I want to createNewFile with a path but I got an IOException. The question is, the detailed message cannot be interpreted, I can only see a bunch of question marks.

IOException – 详细消息全是问号

I am with Windows 10 originally in Spanish, but with Chinese language pack installed. The java language already set to en and file encoding UTF-8:

java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

Why? Only this exception cannot be read.

EDIT: tried to define the language as zh, but does not work.

Use this main class to reproduce it:

public class PromotionTargetFileHandlerMain {
    public static final String uploadingDir = String.join(File.separator,
            System.getProperty("user.dir"), "targets_csv");
    private static final File destDir = new File(uploadingDir);

    public static void main(String[] args) {
        createFileDestination("target.csv");
    }

    public static void createFileDestination(String filename) {
        if (!destDir.exists()) {
            try {
                Files.createDirectory(Path.of(uploadingDir));
            } catch (FileAlreadyExistsException e) {
                log.trace("File dir already exists: {}", uploadingDir);
            } catch (IOException e) {
                log.error("Cannot create temp file dir {}", uploadingDir, e);
                throw new RuntimeException(e);
            }
        }
        String saveLocation = String.join(File.separator,
                uploadingDir, filename
        );
        File saveFile = new File(saveLocation);
        if (saveFile.exists()) saveFile.delete();
        try {
            saveFile.createNewFile();   // <--------------- here IOException
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

答案1

得分: 0

我可能找到了一个解决方案:在控制面板中更改 Windows 10 中非 Unicode 应用程序的区域设置。之前是西班牙语,但现在我改成了中文,然后我就能看到一些其他 IO 错误的本地化消息(无法重现问题中的相同错误):

IOException – 详细消息全是问号

我找到了这个链接

> ...并且 .NET 异常消息将被本地化,而不管消息的预期接收者是谁。

所以我认为这个过程是这样的:

  • 本地操作系统级别的错误消息始终是中文的
  • 由于针对非 Unicode 的操作系统区域设置(为什么 Java 被视为非 Unicode 应用程序,我不清楚),它想将其解释为西班牙语,但失败了
  • 所以我只能看到 ??????

在 Windows 10 中,你可以在搜索栏中输入“控制面板”,然后转到:区域 -> 管理(选项卡) -> 非 Unicode 应用程序语言,设置为中文(或其他与你的本地语言包相对应的语言),然后重新启动。


当然,它也会影响其他非 Unicode 应用程序。

英文:

I may found one solution: change locale in Windows 10 for non-Unicode applications in Control Panel. It was Spanish, but now I changed to Chinese, then I can see some localized message of other IO errors(cannot reproduce the same error in the question):

IOException – 详细消息全是问号

I found this:

> ...and .NET exception messages will be localized regardless of the intended recipient of the message.

So it leads me to believe that the process is like this:

  • native OS level error message is always in Chinese
  • and, due to OS locale setting for non-Unicode(why Java is considered non-Unicode application, I don't know), it wants to interprete it as Spanish, but failed
  • so I can only see ??????

In Windows 10, you can type "Control Panel" in the search bar, and go to: Region -> Management(tab) -> non-Unicode application language, set to Chinese(or others as in your local language pack), and restart.


Of course, it affects other non-Unicode application, too.

答案2

得分: -4

在 try-catch 块中编写你的代码,如果你的文件对象没有问题,你将会得到结果。

try {
    // 代码
} catch (Exception e) {
    println("异常:" + e);
}
英文:

Write your code in try catch blog and in catch (Exception e) then if your file objecct has no issues then you will get the results

  try{
//code
}catch(Exception e){

println("Exception"+e)
}

huangapple
  • 本文由 发表于 2020年8月28日 18:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63632482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定