package com.spd.ORM;
import java.io.*;
import java.util.Properties;
public class Test {
public static void main(String[] args) {
String path = "db.properties";
/* 以文本形式读取配置文件 */
try(BufferedReader br =
new BufferedReader(
new InputStreamReader(
new FileInputStream(path)))) {
String flush;
while ((flush = br.readLine()) != null) {
System.out.println(flush);
}
System.out.println("能够成功地读取文件内容,说明文件路径不存在问题");
} catch (FileNotFoundException e) {
System.err.println("文件不存在!!");
} catch (IOException e) {
System.out.println("IO出现异常");
e.printStackTrace();
}
/* 以配置文件形式读取配置文件 */
Properties pros = new Properties();
try {
pros.load(Thread.currentThread().
getContextClassLoader().getResourceAsStream(path));
} catch (Exception e) {
System.err.println("配置文件连接失败!!");
e.printStackTrace();
}
}
}

java加载配置文件时报空指针异常
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-sinJack 2021-06-27 19:14关注
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));这句代码空指针异常吧
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报