@Test
public void testClass()
{
try {
Class class1 = Class.forName("com.object.Person");
System.out.println(class1.getPackage());
Object object =class1.newInstance();
System.out.println(object);
File file =new File("src/com/ClassTest/hello.txt");
OutputStream out =new FileOutputStream(file);
out.write(123);
out.close();
System.out.println();
Properties properties =new Properties();
InputStream inputStream = ClassTest.class.getClassLoader().
getResourceAsStream("com/ClassTest/hello.txt");//com/PropertiesTest/jdbc.properties
System.out.println(inputStream);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
圖中紅色部分爲測試代碼,
File在創建文件時,是根據自身設定的路徑決定的(Properties->Resource->location,爲根目錄),因此,文件若是想存放在其他子目錄下,需要使用反斜槓;
然而,使用類加載器讀取文件時,讀取文件的路徑爲根目錄下的src文件夾,(關係代碼: ClassTest.class.getClassLoader()(此處也可以使用this.getClass/getClassLoader()代替))。
此處記下筆記。