1、编译器运行时获取配置文件方式
StringBuilder pathBuilders = new StringBuilder(System.getProperty("user.dir"));//获取当前运行项目的路径
pathBuilders.append(File.separator);//添加分隔符
pathBuilders.append("\src\main\propertieNames.properties");//文件在项目中具体的位置
try
{
InputStream inputStream = new FileInputStream(pathBuilders.toString());
properties = new Properties();
properties.load(inputStream);
}
catch (IOException e)
{
}
2、打包时获取配置文件的路径为bin目录下
StringBuilder pathBuilder = new StringBuilder(getClass().getResource("/").getFile().toString());//获取程序打包后的bin文件夹路径
pathBuilder.append(File.separator);
pathBuilder.append("propertieNames.properties");//配置文件名称
Properties properties = null;
try
{
InputStream inputStream = new FileInputStream(pathBuilders.toString());
properties = new Properties();
properties.load(inputStream);
}
catch (IOException e)
{
}