读取properties配置文件路径找不到的解决方法

public class DB_connection {
    String JDBC_DRIVER;//="com.mysql.jdbc.Driver";  
    String DB_URL;// = "jdbc:mysql://localhost:3306/promotion_website?useUnicode=true&characterEncoding=UTF-8";
    String FILENAME = "/database.properties";
    

    String USER;// = "root"; 
    String PASS;// = "mysql";
    Connection conn = null;

    public DB_connection() {
        try {
            Properties prop = new Properties();
            String FILE_PATH =  this.getClass().getClassLoader().getResource(FILENAME).getPath();
            System.out.println("path  "+FILE_PATH);
            File file = new File(FILE_PATH);
            FileInputStream in = new FileInputStream(file);
            prop.load(in);
            JDBC_DRIVER = prop.getProperty("JDBC_DRIVER");
            DB_URL = prop.getProperty("DB_URL");
            USER = prop.getProperty("USER");
            PASS = prop.getProperty("PASS");
                    
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DB_connection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DB_connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

.................................
省略
...................................
}

就是使用类装载器来获取配置文件的绝对路径
输出结果是
/home/peng/文档/PROGRAM/WEBsite/promotion/build/web/WEB-INF/classes/database.properties

Properties prop = new Properties();
String FILE_PATH = this.getClass().getClassLoader().getResource(FILENAME).getPath();
System.out.println("path "+FILE_PATH);
File file = new File(FILE_PATH);
FileInputStream in = new FileInputStream(file);
prop.load(in);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值