读取properties.xml配置文件的类

前端之家收集整理的这篇文章主要介绍了读取properties.xml配置文件的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public class Utils {
  2. protected final static Logger log = Logger.getLogger(Utils.class);
  3. private static Properties props = new Properties();
  4.  
  5. // 读取配置文件
  6. public static void readFile(String filePath) {
  7. try {
  8. InputStream in = new BufferedInputStream(new FileInputStream(
  9. filePath));
  10. props.load(in);
  11. } catch (Exception e) {
  12. log.debug("配置文件不存在");
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. /**
  18. * @description:读取key对应的value
  19. * @author:yehui
  20. * @return:String
  21. */
  22.  
  23. public static String readValue(String key) {
  24. String value = props.getProperty(key);
  25. log.debug("key:"+value);
  26. return value;
  27. }
  28.  
  29. //读取properties中全部内容
  30. public static void readProperties(String filePath) {
  31. Properties props = new Properties();
  32. try {
  33. InputStream in = new BufferedInputStream(new FileInputStream(
  34. filePath));
  35. props.load(in);
  36. Enumeration en = props.propertyNames();
  37. while (en.hasMoreElements()) {
  38. String key = (String) en.nextElement();
  39. String Property = props.getProperty(key);
  40. System.out.println(key + Property);
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. public static Logger getLog() {
  48. return log;
  49. }

只需要传properties文件的path即可

借鉴了别人的代码

猜你在找的XML相关文章