
Properties prop = new Properties();
String path = AlarmController.class.getResource("/").getPath(); //AlarmController在哪个类里面写的就写哪个类
System.out.println(path); ///D:/workspace/cxfvp/src/main/webapp/WEB-INF/classes/ property放在这里
String websiteURL = (path+ "area.properties"); //得到自己的文件位置
try{
//读取属性文件area.properties
InputStream in = new BufferedInputStream (new FileInputStream(websiteURL));
prop.load(in); ///加载属性列表
String jinhua = prop.getProperty("jinhua"); //取value
String hangzhou = prop.getProperty("hangzhou");
String ningbo = prop.getProperty("ningbo");
System.out.println(jinhua);
}
catch(Exception e){
System.out.println(e);
}
配置文件
本文介绍了一个Java程序示例,演示了如何通过类路径获取资源文件的位置,并使用Properties类来读取属性文件的内容。具体步骤包括定位属性文件、加载文件到Properties对象以及获取指定属性的值。
1187

被折叠的 条评论
为什么被折叠?



