解决java项目打包可执行jar包后 找不到配置文件问题

1、项目结构及配置文件内容

 

 2、配置文件内容读取 代码实现

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

 

public class TestResource
{
public static void main(String[] args)
{
TestResource t=new TestResource();
t.test();
}

public void test()
{
InputStream in = TestResource.class.getClass().getResourceAsStream("/resource/config.properties");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
}
catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
}

Properties p = new Properties();

try
{
p.load(reader);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(p.getProperty("name"));
System.out.println(p.getProperty("name1"));

//System.out.println(p.getProperty("excelPath").toString().trim());
//System.out.println(p.getProperty("driverPath").toString().trim());

}
}

3、执行结果

 

 4、执行jar包

 

posted @ 2020-09-07 16:26  peachlf  阅读(5472)  评论(0编辑  收藏  举报