web工程中普通 java 类使用 properties 配置文件中属性

来源:互联网 发布:网络科学导论 百度云 编辑:程序博客网 时间:2024/06/02 09:23
//web工程中普通 java 类使用 properties 配置文件中属性的方法:<pre name="code" class="html"><pre name="code" class="html">//获取当前 java 类所在磁盘绝对路径,拼接配置文件所在磁盘路径。//代码如下:
try {//配置文件绝对路径:F:/HYPrograms/tomcat/apache-tomcat-6.0.41/webapps/webDemo/WEB-INF/config/db.properties//获取当前类所在磁盘完整路径String path = this.getClass().getResource("").getPath(); //拼接配置文件路径 path = path.substring(0, path.indexOf("WEB-INF")) + "WEB-INF/config/db.properties"; prop.load(new FileInputStream(path));classDriver = prop.getProperty("classDriver");url = prop.getProperty("url");username = prop.getProperty("username");password = prop.getProperty("password");System.out.println(classDriver + "," + url + "," + username + "," + password);} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}


0 0