可能是getResourceAsStream中的路径不对,导致没有读取到配置文件。参考下面的代码: package com; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Test { public static void main(String[] args) throws IOException { System.out.println(Test.class.getResource("/"));//file:/D:/scsworkspace/Test/build/classes/ System.out.println(Test.class.getResource(""));//file:/D:/scsworkspace/Test/build/classes/com/\ /* * 用Test.class.getResourceAsStream("dev.properties")找不到改文件,如果一定要使用,把配置文件放到com包下。 */ InputStream input = Test.class.getResourceAsStream("/dev.properties");// Properties prop = new Properties(); prop.load(input); System.out.println(prop.get("address"));//127.0.0.1 } }
可能是getResourceAsStream中的路径不对,导致没有读取到配置文件。
参考下面的代码: