Paul - The Programmer

simple & stupid
Setup a Linux PyS60 development environment
Alter the word order in lines by VIM regular expression

The proper way to access to file in resource by Java

paul posted @ Tue, 08 Mar 2011 22:33:35 +0800 in coding life with tags java Resource File , 6800 readers

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.  ;-)

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter