Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

linux - How to fix java when if refused to open a file with special character in filename?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...