public class PropertiesTest {
public static void main(String[] args) {
Properties properties = new Properties();
try {
//查看当前类的Path
File classPath = new File("");
String classAbsolutePath = classPath.getAbsolutePath();
String classCnoPathString = classPath.getCanonicalPath();
System.out.println("classAbsolutePath="+classAbsolutePath);
System.out.println("classCnoPathString="+classCnoPathString);
//读取src目录下的properties文件
InputStream inputStreamPro = PropertiesTest.class.getClassLoader().getResourceAsStream("test.properties");
//读取test.properties配置文件
InputStream inputStream = new BufferedInputStream(inputStreamPro);
properties.load(inputStream);
Iterator<String> iterator = properties.stringPropertyNames().iterator();
while(iterator.hasNext())
{
String key = iterator.next();
System.out.println(key+" : "+properties.getProperty(key));
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java读取.properties文件
最新推荐文章于 2024-09-13 17:30:19 发布