1、通过类加载器加载
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Chapter8/test.properties"); Properties p = new Properties(); p.load(inputStream );
2、通过文件系统加载
InputStream inputStream = new FileInputStream("Chapter8/test.properties");
以下是获取当前工程路径的方法:
public static void main(String[] args) { // TODO Auto-generated method stub path[0] = Thread.currentThread().getContextClassLoader() .getResource("Chapter8/test.properties").getPath(); path[1] = this.getClass().getClassLoader() .getResource("Chapter8/test.properties").getPath(); path[2] = ClassLoader.getSystemResource("Chapter8/test.properties") .getPath(); }
时间: 2024-10-13 05:42:45