通过类加载器获取已知文件名的路径
通过文件名获得路径和绝度路径
1 public class ClassloderGetpath { 2 public static void main(String[] args) throws IOException { 3 //创建properties 4 Properties pro = new Properties(); 5 6 //通过src下的文件名获取绝对路径 7 URL resource = InputStreamDemo.class.getClassLoader().getResource("prop.properties"); 8 String path = resource.getPath(); 9 System.out.println(path); 10 11 //使用类加载器获得输入流 12 InputStream resourceAsStream = ClassloderGetpath.class.getClassLoader().getResourceAsStream("abcd.properties"); 13 //将类加载 14 pro.load(resourceAsStream); 15 //获取键值对 16 String username = pro.getProperty("username"); 17 18 System.out.println(username); 19 } 20 }