原来是这么写的:
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("db.properties");
// 使用properties对象加载输入流
properties.load(in);
用这种办法读取配置文件(.properties文件)时,当你将项目导出成jar包时,是无法读取外部的配置文件(.properties文件),所以我们将改成读取外部的配置文件(.properties文件),代码如下:
Properties p = new Properties();
FileInputStream fis = new FileInputStream("db.properties");
p.load(fis);
解决方法:把代码改成第二种的,然后把jar文件和.properties文件放在同一路径,这样子就可以读取到.properties配置文件