Possible bugs with Java file system and non latin characters in file names

Alan Bateman Alan.Bateman at oracle.com
Sun Oct 30 06:38:13 PDT 2011


On 30/10/2011 11:56, Martijn Verburg wrote:
> Hi all,
>
> I ran across some legacy I/O code that was causing some issues for a client.
>
> I tried to see if this was already reported in the bugs database, but
> I got a NPE from the Glassfish server trying to log in (the irony ;p),
> so I thought l should report it here.  Under Java 6u29 and Java 7u1
> the following sample code (which simply creates a FileReader and
> prints out each file in a directory) behaves oddly when faced with a
> file in the directory of the name Bérletmozgás-201110.txt.
>
> ---------SSCE----------
>
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.FileReader;
>
> public class NonLatinFilenameBugExample
> {
>      private static final String DIR = "/temp/";
>      //on Windows: private static final String inbox = "c:/temp/";
>
>      // Could pass in the path, but hard coded it for SSCE
>      public static void main(String[] args)
>      {
>          File dir = new File(DIR);
>          File[] files = dir.listFiles();
>          for (int i = 0; i<  files.length; i++)
>          {
>              try
>              {
>                  new FileReader(files[i].getAbsolutePath());
>                  System.out.println(files[i].getAbsolutePath());
>              }
>              catch (FileNotFoundException e)
>              {
>                  // Dump the stack trace as proof
>                  e.printStackTrace();
>              }
>          }
>      }
> }
>
> * It works fine under Windows listing the file
> * It produces a FileNotFoundException under Linux and Mac OS X Lion
> when the new FileReader(files[i].getAbsolutePath()); call is made
> * Files of any type/extension/name are not discovered by the
> dir.listFiles(); call under cygwin
>
> Does this sound familiar to anyone at all?
>
This is the long standing with java.io.File using a String to represent 
a file name when the underlying platform or file system uses bytes 
(http://bugs.sun.com/view_bug.do?bug_id=4899439 has some background).

You are usually work around this by changing your locale ( possibly 
hu_HU for Hungarian in this case). Alternatively change the code to use 
the new API. For this code fragment then replace the listFiles and for 
loop with the following:

try (DirectoryStream<Path> stream = 
Files.newDirectoryStream(dir.toPath())) {
     for (Path entry: stream) {
         try (Reader reader = Files.newBufferedReader(entry, 
Charset.defaultCharset())) {
         }
     }
}

and it should do what you expect.

-Alan.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20111030/276865c9/attachment.html 


More information about the nio-dev mailing list