java.io.FileNotFoundException:解决方法
java.io.FileNotFoundException:解决方法运行如下代码会出现java.io.FileNotFoundException:D:\IDEA%20Workspace\out\production\day04_JDBC\jdbc.properties (系统找不到指定的路径。)```javapublic class test {public static void main(St
·
java.io.FileNotFoundException:解决方法
运行如下代码会出现
java.io.FileNotFoundException:D:\IDEA%20Workspace\out\production\day04_JDBC\jdbc.properties (系统找不到指定的路径。)
```java
public class test {
public static void main(String[] args) {
try {
//1.创建properties集合类
Properties properties = new Properties();
//获取src路径下的文件的方式--->ClassLoader 类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
//jdbc.properties 绝对路径:D:\\IDEA Workspace\\CZHM\\JavaWeb\\day04_JDBC\\src\\jdbc.properties
URL resource = classLoader.getResource("jdbc.properties");
String path = resource.getFile();
// /D:/IDEA%20Workspace/out/production/day04_JDBC/jdbc.properties
System.out.println(path);
//2. 加载文件
// pro.load(new FileReader("D:\\IdeaProjects\\CZHM\\day04_jdbc\\src\\jdbc.properties"));
properties.load(new FileReader(path));
// properties.load(new FileReader("D:\\IDEA Workspace\\CZHM\\JavaWeb\\day04_JDBC\\src\\jdbc.properties"));
//3. 获取数据,赋值
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
System.out.println(url);
System.out.println(user);
System.out.println(password);
System.out.println(driver);
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决办法:
String path = resource.getFile();
//添加如下代码
path = java.net.URLDecoder.decode(path, "utf-8");
即可解决
更多推荐
所有评论(0)