我的目录结构
使用绝对路径加载
Properties p = new Properties();
InputStream in = null;
try {
//用绝对路径加载
File file =new File("C:\\Users\\Plory\\IdeaProjects\\a\\src\\main\\java\\package2\\p2.properties");
in = new FileInputStream(file);
p.load(in);
System.out.println("username="+p.getProperty("username"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
通过Class.getResourceAsStream(String path)加载
Properties p = new Properties();
//从classpath根目录下加载文件
//InputStream in = package2.Test.class.getClassLoader()