Java 初学者-javareflect05
今天学习了什么?
import java.io.FileReader; import java.util.Properties; public class IoProperties { public static void main(String[]args) throws Exception{ String path=Thread.currentThread().getContextClassLoader().getResource("classinfor.properties").getPath(); FileReader fr=new FileReader(path); // InputStream reader=Thread.currentThread().getContextClassLoader().getResourceAsStream("classinfor.properties"); //直接以流的形式返回 Properties pro=new Properties(); pro.load(fr); //fr.close(); String className=pro.getProperty("className"); System.out.println(className); Class c=Class.forName(className); Object obj=c.newInstance(); System.out.println(obj); } }
import java.io.FileReader; public class PathProblem { public static void main(String[]args) { //按相对路径缺点是移植性差,默认是当前project的根 //FileReader fr=new FileReader("src/classinfor.properties"); //使用以下方式的前提是,这个文件必须在类路径下‘ //在src下,src是类的根路径 //Thread.currentThread()当前线程对象 //是线程对象的方法,可以获取到线程对象的类加载器对象 //getResource(name)类加载器的方法,默认从类的根路径加载文件 String path=Thread.currentThread().getContextClassLoader().getResource("classinfor.properties").getPath(); System.out.println(path); //其它文件时String path=Thread.currentThread().getContextClassLoader().getResource("com/bjpowernode/user/classinfor.properties").getPath(); } }
明天学习什么?
明天计划学习对象序列化与反序列化。