配置文件路径如何写

来源:互联网 发布:华北电力大学网络教育 编辑:程序博客网 时间:2024/05/17 01:32

Demo1.java

package com.husky.path;import java.io.FileReader;import java.io.InputStream;import java.util.Properties;/** * 配置文件的路径应该如何去写? * 绝对路径:一个文件的完整路径信息。一般绝对路径带盘符,无法使用 * 相对路径:相对于当前程序的路径。当前路径就是执行java命令的时候,控制台的位置 * 类文件路径:类文件路径就是使用了classpath的路径找到相应的资源 * 如果需要使用类文件路径首先要获取一个Class对象 * * * */public class Demo1 {    static Properties properties;    static{        try{            properties = new Properties();            Class clazz = Demo1.class;            InputStream inputStream = clazz.getResourceAsStream("/db.properties");            properties.load(inputStream);        }catch (Exception e){            e.printStackTrace();        }    }    public static void main(String[] args){        System.out.println("username:"+properties.getProperty("user")+"密码:"+properties.getProperty("password"));    }}

db.properties

user=huskypassword=dabendan