1. 程式人生 > >用getResoure()方法獲取文件路徑要把配置文件放在src根目錄。

用getResoure()方法獲取文件路徑要把配置文件放在src根目錄。

tcl path code 獲取 etc student ava resource bsp

String path = demo1.class.getClassLoader().getResource("student.xml").getPath();


Exception in thread "main" java.lang.NullPointerException;

配置文件不放在src根目錄下會報空指針異常,提示並非文件找不到。也就是默認目錄是src根目錄,相對路徑可根據src來調整。


 String path = demo1.class.getClassLoader().getResource("student.xml").getPath();

用getResoure()方法獲取文件路徑要把配置文件放在src根目錄。