Functions from java.io solely depend on the locale settings of your operating system. If you want to create the directory /tmp/foö/bär, save this snippet to a file called javaapplication1/JavaApplication1.java:
package javaapplication1; public class JavaApplication1 { static { System.setProperty("file.encoding", "UTF-8"); System.setProperty("sun.jnu.encoding", "UTF-8"); } public static void main(String[] args) { new java.io.File("/tmp/foö/bär").mkdirs(); } }Since the source file contains UTF-8 characters, the Java compiler needs to be aware of it:
$ javac -encoding UTF-8 javaapplication/JavaApplication1.javaNow run that program:
$ java -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 javaapplication.JavaApplication1If your environment is set to ASCII or not set at all, you will get /tmp/fo?/b?r. If your environment is set to ISO-8859-1 instead, you will get /tmp/fo\366/b\344r. System properties given on the commandline and in the static initialization block have no effect. You have to set your environment to an *.utf-8 locale:
$ LC_ALL=de_DE.utf-8 java javaapplication.JavaApplication1Now you have a directory tree named /tmp/f\303\266/b\303\244r, which is the correct byte level encoding for UTF-8.
In contrast to java.io, java.nio honors the sun.jnu.encoding system property set in the static initialization block:
package javaapplication1; public class JavaApplication1 { static { System.setProperty("sun.jnu.encoding", "UTF-8"); } public static void main(String[] args) throws java.io.IOException { java.nio.file.Files.createDirectories(java.nio.file.Paths.get("/tmp/foö/bär")); } }Compilation is not different:
$ javac -encoding UTF-8 javaapplication/JavaApplication1.javaBut, you can omit any locale settings and system properties on the command line, and will still get directories with UTF-8 names:
$ java javaapplication.JavaApplication1If you remove the static initialization block, e.g.
package javaapplication1; public class JavaApplication1 { public static void main(String[] args) throws java.io.IOException { java.nio.file.Files.createDirectories(java.nio.file.Paths.get("/tmp/foö/bär")); } }and if your environment is not set to a *.utf-8 locale, then you have to set it manually again:
$ LC_ALL=de_DE.utf-8 java javaapplication.JavaApplication1Btw, java.nio just defines static functions plus several interfaces. For an object oriented language I find this highly unclean.