2012年10月23日 星期二

getResource() function with classpath

1. Why use getResource() function ? 
    Sometimes there is a need for you to get file, but in java the constructor of File class need the absolute path, but it is depends on the machine, it will be a problem when you didn't know the actual parent path in other computer.
    Therefore, you need a method supporting defining the different root staring path, and can get the file. That is the answer of the  getResource function.


2. where is the relevant searching path in getResource()?
  1.  package as the staring path
  2.  classpath as the staring path

3. Example
File file3 = new File(Test.class.getResource("file3.txt").getFile())



Reference:
http://gavin-chen.iteye.com/blog/261151
 

沒有留言:

張貼留言