配置文件路径如何写

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=husky
password=dabendan

猜你喜欢

转载自blog.csdn.net/qq_32296307/article/details/78534761
今日推荐