public class OrderService { private static Logger logger = Logger.getLogger(OrderService.class); private static Properties props = null; static { props = new Properties(); try { InputStream in = OrderService.class.getClass().getResourceAsStream("/status.properties"); props.load(in); } catch (Exception e) { logger.error("状态资源文件未加载" + e.getMessage()); } } public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>((Map) props); Set propertySet = map.entrySet(); for (Object o : propertySet) { Map.Entry entry = (Map.Entry) o; System.out.printf("%s = %s%n", entry.getKey(), entry.getValue()); } } }
status.base.order10 = CLOSE_REFUND status.base.order7 = PAID_UNSENT status.base.order8 = PAID_TO_CONFIRM
本文介绍了一个Java程序如何加载并遍历属性文件(status.properties)的内容。通过静态代码块初始化Properties对象,并从类路径下的特定文件中读取键值对。主方法展示了如何将这些键值对转换为Map并打印出来。
1万+

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



