我的配置文件放在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();
}
}
文件路径,你把 properties 放在 src 目录下了,那么它会被编译放到 classes 类路径下的。就需要通过类路径的方式获取。
这里看起来是 web 应用,set.properties 会被编译到类路径下的。
web项目的话,根目录应该是在web文件夹下。 你放的src是编译class的类路径,不是web项目的根目录,所以直接写文件名应该获取不到。 挪到web的根目录就好了。可以在webapp的根目录创建一个prop的文件夹来存放
https://blog.csdn.net/qq_39247931/article/details/93416716
把路径改为绝对路径重新编译一下,建议在电脑中找到这个文件,复制在电脑中的文件路径,粘贴过来,重新编译运行一下试试