3

I'm currently testing how Java handles directories containing utf-8 characters. For this I've created a folder containing 10.000 folders with random names like this: 0dgý lEEóæ2êOðuKõþþÞbA.

I verified that 10.000 folders exist in the terminal:

/folder_test$ find ./* -maxdepth 1 -type d | wc -l
10000

My Java code looks like this:

File[] listFiles = path.toFile().listFiles();
System.out.println(listFiles.length);
for (File file : listFiles) {
    System.out.println(file);
}
try {
    System.out.println(Files.list(path).count());
} catch (IOException ex) {
    ex.printStackTrace();
}

listFiles.length tells me that it only has 7289 elements.

The Files.list(path).count() lines fails with the following Exception:

java.nio.file.FileSystemException: /folder_test: {d��
    at java.nio.file.Files$2.hasNext(Files.java:3462)
    at java.util.Iterator.forEachRemaining(Iterator.java:115)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.LongPipeline.reduce(LongPipeline.java:438)
    at java.util.stream.LongPipeline.sum(LongPipeline.java:396)
    at java.util.stream.ReferencePipeline.count(ReferencePipeline.java:526)
        ........

Locale.getDefault() is en_US and CharSet.defaultCharset() is UTF-8.

Why does Java fail while bash and my file explorer seem to handle the directories just fine?

fap
  • 663
  • 1
  • 5
  • 14
  • Here is some info you may find useful to further investigate and understand the issue: [special-characters-in-filename-java](https://stackoverflow.com/questions/28476663/special-characters-in-filename-java) and [this article](https://community.oracle.com/thread/2076851) at Oracle's tech forum. – prasad_ Sep 20 '18 at 00:55

0 Answers0