如果拒绝打开文件名中含有特殊字符的文件时如何解决java? [英] How to fix java when if refused to open a file with special character in filename?

查看:113
本文介绍了如果拒绝打开文件名中含有特殊字符的文件时如何解决java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开一个文件,使用Java拒绝打开的特殊字符?

在开始的时候,我猜这是一个字符集编码问题,因为我读取文件名从日志文件错误。但后来我发现,这可能是一个JVM的bug,我需要一个解决方法。



真正的例子更好的话

  import java.io. *; 
public class WTF {
public static void main(String [] s)throws Exception {
File f2 = new File(。);
for(File subFile:f2.listFiles()){
System.out.println(subFile.getName());
System.out.println(subFile.exists());
System.out.println(new FileInputStream(subFile));





结果

  [USER @ SERVER ZZZ] $ java -cp。 WTF 
WTF.class
true
java.io.FileInputStream@732dacd1
WTF.java
true
java.io.FileInputStream@3bad086a
ABC_ %81DEF.txt
false
线程main中的异常java.io.FileNotFoundException:ABC_ %81DEF.txt(没有这样的文件或目录)
在java.io. < init>(FileInputStream.java:106)
at java.io.FileInputStream。< init>(FileInputStream.java:66(FileInputStream.open(Native Method))
at java.io.FileInputStream。 )
在WTF.main(WTF.java:8)

并且该文件夹包含

  [USER @ SERVER ZZZ] $ ls -lb 
-rw -r - r-- 1用户组8 Apr 14 20:54 ABC_\303%81DEF.txt
-rw -r - r-- 1 USER GROUP 1068 Apr 14 20:58 WTF.class
-rw-r - r- - 1 USER GROUP 554 Apr 14 20:58 WTF.java


解决方案

<它可能是相关的 File.exists()以unicode字符名称失败


How to open a file, with a special character which Java refused to open?

At the beginning I guess it was a charset encoding problem as I read the filename wrong from a log file. But later I found that, it is possible a bug of JVM and I need a workaround.

Real example better then words

import java.io.*;
public class WTF{
        public static void main(String[] s)throws Exception{
                File f2=new File(".");
                for (File subFile : f2.listFiles()) {
                        System.out.println(subFile.getName());
                        System.out.println(subFile.exists());
                        System.out.println(new FileInputStream(subFile));
                }
        }
}

With a result

[USER@SERVER ZZZ]$ java -cp . WTF
WTF.class
true
java.io.FileInputStream@732dacd1
WTF.java
true
java.io.FileInputStream@3bad086a
ABC_�%81DEF.txt
false
Exception in thread "main" java.io.FileNotFoundException: ABC_�%81DEF.txt (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at WTF.main(WTF.java:8)

And the folder contains

[USER@SERVER ZZZ]$ ls -lb
-rw-r--r-- 1 USER GROUP    8 Apr 14 20:54 ABC_\303%81DEF.txt
-rw-r--r-- 1 USER GROUP 1068 Apr 14 20:58 WTF.class
-rw-r--r-- 1 USER GROUP  554 Apr 14 20:58 WTF.java

解决方案

Could it be related to File.exists() fails with unicode characters in name

这篇关于如果拒绝打开文件名中含有特殊字符的文件时如何解决java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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