如图Test类加载config.properties配置文件,且保证打包后也能正常读取
import java.io.InputStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) {
//1.读取配置文件
Properties properties = new Properties();
try {
InputStream inputStream = Test.class.getResourceAsStream("/config.properties");
// 如果配置文件在项目的根目录下
// input = new FileInputStream("config.properties");
properties.load(inputStream);
System.out.println("配置文件已成功添加!");
} catch (Exception e) {
e.printStackTrace();
System.err.println("无法添加配置文件:" + e.getMessage());
}
System.out.println("配置文件内容为:" + properties.getProperty("file.path"));
}
}
如果在resource目录下,则地址换成/resource/config,properties