The proper way to access to file in resource by Java
The below code demonstrates how to access to a resource file.
import java.io.*; public class FileTest{ public void test() throws IOException { String filePath = this.getClass().getResource("/test.txt").getPath(); System.out.println( filePath ); System.out.println( file.exists() ); } public static void main(String [] args) throws Exception { FileTest test = new FileTest(); test.test(); } }
Try to execute:
~/lab/test$ java FileTest /home/paul/lab/test/test.txt true
It seems work perfectly. But this code is acturally not 100% right. When the class file is executed in a directory which name contains space character, the resource file will not be found.
~/lab/my test$ java FileTest /home/paul/lab/my%20test/test.txt false
The space character in the file path was replaced by the '%20'. So, I update the path string before use it to create the File object.
import java.io.*; public class FileTest{ public void test() throws IOException { String filePath = this.getClass().getResource("/test.txt").getPath(); System.out.println( filePath.replace("%20", " ") ); File file = new File(filePath.replace("%20", " ")); System.out.println( file.exists() ); } public static void main(String [] args) throws Exception { FileTest test = new FileTest(); test.test(); } }
Try this again:
~/lab/my test$ java FileTest /home/paul/lab/my test/test.txt true
Looks like it's all right this time. Even the space charater in pah is supported now.
But this is still not the best way.
The above code acturally use the URL to access to the file. But the best way is use URI to access to file. Only the URI can directly support both the space and Chinese characters in file path.
import java.io.*; public class FileTest{ public void test() throws IOException, URISyntaxException { System.out.println( this.getClass().getResource("/test.txt").toURI( ).getPath()); File file = new File(this.getClass().getResource("/test.txt").toURI()); System.out.println( file.exists() ); } public static void main(String [] args) throws Exception { FileTest test = new FileTest(); test.test(); } }
The execution result:
~/lab/my test$ java FileTest /home/paul/lab/my test/test.txt true
It works properly and the code is more simple with the URI.
The above code does not support to access to the files in another Jar file's resouce. But I'd like to talk about this topic in another post. ;-)
Configure javac to report messages in English
On my Chinese version Windows XP, the javac always shows Simple Chinese messages. But I'd rather see the English ones.
The traditional way is to change the default system Local setting, then the JVM local is changed as well. But this solution is too inconvenient and painful.
Fortunately, we do not have to do that. We can pass the '-J-Duser.language' option to the javac to change its JVM Local setting.
e.g.
javac '-J-Duser.language=en -J-Duser.country=GB' -help
The javac will show the help messages in English instead of the Simple Chinese.
Those two options can also be passed in the ant script by adding the <compilerarg> in the <javac>
e.g.
<target name="compile" description="compile hello world"> <mkdir dir="${classes}"/> <javac srcdir="${src}" destdir="${classes}" fork="true" > <compilerarg value="-J-Duser.language=en"/> <compilerarg value="-J-Duser.country=GB"/> </javac> </target>
The attribute fork="true" is mandatory. The two options can only effect the forked compiler.