Java获取不同路径下的配置文件

2019-11-12  本文已影响0人  背对背拥抱

首选,下面是项目的目录结构:

写在前面:
关于相对路径的写法,有两个需要注意的点:(我们此时的类名为GetConfigFilePath

1)this.getClass()GetConfigFilePath.class都能获得类路径,但是this无法在静态代码块中使用

//this无法在静态代码块中使用
InputStream in = this.getClass().getClassLoader().getResourceAsStream("srcConfig.properties");
InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("srcConfig.properties");

2)加不加getClassLoader方法的区别:

InputStream in = GetConfigFilePath.class.getResourceAsStream("/srcConfig.properties");
//调用getClassLoader方法后,无需在前面加左斜杠`/`
InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("srcConfig.properties");
一、配置文件放在src根目录下:
@Test
    public void getSrcConfig() {
        try {
            InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("srcConfig.properties");
            Properties pro = new Properties();
            pro.load(in);
            
            String pathName = pro.getProperty("pathName");
            String path = pro.getProperty("path");
            
            System.out.println(pathName);
            System.out.println(path);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
二、配置文件放在src/conf目录下:
/**
 * 配置文件放在src/conf目录下
 */
@Test
public void getConfConfig() {
    try {
        InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("conf/confConfig.properties");
        Properties pro = new Properties();
        pro.load(in);
        
        String pathName = pro.getProperty("pathName");
        String path = pro.getProperty("path");
        
        System.out.println(pathName);
        System.out.println(path);
        
    } catch (IOException e) {
        e.printStackTrace();
    }
}
三、配置文件放在项目文件夹根目录:
/**
 * 配置文件放在"项目文件夹根目录"下,和src同级
 */
@Test
public void getRootConfig() {
    try {
        String userDir = System.getProperty("user.dir");
        InputStream in = new FileInputStream(userDir+"\\rootConfig.properties");
        Properties pro = new Properties();
        pro.load(in);
        
        String pathName = pro.getProperty("pathName");
        String path = pro.getProperty("path");
        
        System.out.println(pathName);
        System.out.println(path);
        
    } catch (IOException e) {
        e.printStackTrace();
    }

总体代码如下:

package com.zl.configfile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.junit.Test;

public class GetConfigFilePath {
    
    /**
     * 获取src目录下的properties文件
     */
    @Test
    public void getSrcConfig() {
        try {
//          InputStream in = GetConfigFilePath.class.getResourceAsStream("/srcConfig.properties");
//          InputStream in = this.getClass().getClassLoader().getResourceAsStream("srcConfig.properties");
            InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("srcConfig.properties");
            Properties pro = new Properties();
            pro.load(in);
            
            String pathName = pro.getProperty("pathName");
            String path = pro.getProperty("path");
            
            System.out.println(pathName);
            System.out.println(path);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 配置文件放在src/conf目录下
     */
    @Test
    public void getConfConfig() {
        try {
            String userDir = System.getProperty("user.dir");
            InputStream in = GetConfigFilePath.class.getClassLoader().getResourceAsStream("conf/confConfig.properties");
            Properties pro = new Properties();
            pro.load(in);
            
            String pathName = pro.getProperty("pathName");
            String path = pro.getProperty("path");
            
            System.out.println(pathName);
            System.out.println(path);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 配置文件放在"项目文件夹根目录"下,和src同级
     */
    @Test
    public void getRootConfig() {
        try {
            String userDir = System.getProperty("user.dir");
            InputStream in = new FileInputStream(userDir+"\\rootConfig.properties");
            Properties pro = new Properties();
            pro.load(in);
            
            String pathName = pro.getProperty("pathName");
            String path = pro.getProperty("path");
            
            System.out.println(pathName);
            System.out.println(path);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

上一篇下一篇

猜你喜欢

热点阅读