我的配置文件放在src目录下,名字叫做jdbc.properties,读取配置文件的时候使用的代码是InputStream inputStream = new FileInputStream("jdbc.properties");但是当我运行的时候报错 java.io.FileNotFoundException: jdbc.properties (系统找不到指定的文件。)
从网上找了下问题,可能是我的相对路径不对,遂改为下面代码这样。 新建一个工程测试时发现这样是可以读取到配置文件的。但是运行这个程序的时候依然报错java.io.FileNotFoundException: jdbc.properties (系统找不到指定的文件。)
我的路径写错了吗?可是我已经修改了文件的名字为set.properties,就算路径有错但为什么报错的时候还是说找不到jdbc.properties?
我是用的是intellij IDEA,这个是个web项目,使用的tomcat服务器,非常感谢解答!
public class JDBCUtil {
private static String driverClass;
private static String url;
private static String name;
private static String password;
private JDBCUtil() {
}
static {
try {
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("./src/set.properties");
properties.load(inputStream);
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}