Java File.listFiles () returns files that "don't exist" according to `exists ()`

I noticed this problem in our productive code:

java.lang.IllegalArgumentException: /somePath/ .png does not exist
    at org.apache.commons.io.FileUtils.sizeOf(FileUtils.java:2413)
    at org.apache.commons.io.FileUtils.sizeOfDirectory(FileUtils.java:2479)

The main reason is as follows:

import java.io.File;

public class FileNameTest
{

    public static void main(String[] args)
    {
        File[] files = new File("/somePath").listFiles();
        for (File file : files)
        {
            System.out.println(file + " - " + (file.exists() ? "exists" : "missing!!"));
        }
    }

}

Conclusion:

0.png - exists
7.png - exists
4.png - exists
8.png - exists
1.png - exists
3.png - exists
 .png - missing!!
2.png - exists
5.png - exists
 .png - missing!!
6.png - exists
d.png - exists
$.png - exists
s.png - exists
+.png - exists
9.png - exists

The "missing" files are called the symbols "μ" (Mu) and "€" (Euro) .

It also seems that these file names use the wrong encoding. When I list files in bash, they also do not display correctly. When I convert the output lsfrom latin1 to UTF-8, they display correctly (at least mu).

But nonetheless...

  • these files exist
  • file.listFiles () lists them
  • for two special cases: file.exists () returns false

I believe this is a bug in the JVM. Can anyone confirm this?

? , ? ( , .)

:

  • Ubuntu 4.2.0
  • java "1.8.0_102"
  • Java (TM) SE Runtime Environment ( 1.8.0_102-b14)
  • 64- Java HotSpot TM ( 25.102-b14, )
  • Apache Commons IO 2.4
+4
1

, . Java , . Java .

- file.encoding .

EDIT: , , , file.encoding . , - , UTF-8. http://jonisalonen.com/2012/java-and-file-names-with-invalid-characters/

:

+3

Source: https://habr.com/ru/post/1657798/


All Articles