在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

properties文件转为json字符串

2016-12-20 13:13| 发布者: zhangjf| 查看: 1574| 评论: 0

摘要: properties文件转为json字符串 import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import ...
properties文件转为json字符串
                    
                  
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.util.Properties;

  4. import org.apache.commons.io.IOUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;

  7. import com.alibaba.fastjson.JSON;
  8. import com.google.common.io.Resources;

  9. /**
  10. * properties文件转为json字符串
  11. *
  12. * @author James
  13. * @version $Id: MyPropToJson.java, v 0.1 2016年8月22日 下午4:17:18 James Exp $
  14. */
  15. public class MyPropToJson {

  16.     private static final Logger LOGGER = LoggerFactory.getLogger(MyPropToJson.class);

  17.     /**
  18.      *
  19.      * @param args
  20.      */
  21.     public static void main(String[] args) {
  22.         String resourceName = "config/test.properties";
  23.         System.out.println(toJSONString(resourceName));
  24.     }

  25.     /**
  26.      * properties文件转为json字符串
  27.      *
  28.      * @param resourceName
  29.      *        例如,properties文件位置 ,"config/xxx.properties"
  30.      */
  31.     public static String toJSONString(String resourceName) {
  32.         InputStream inputStream = null;
  33.         Properties props = new Properties();
  34.         try {
  35.             inputStream = Resources.getResource(resourceName).openStream();
  36.             props.load(inputStream);
  37.         } catch (IOException e) {
  38.             LOGGER.error(e.getMessage(), e);
  39.         } finally {
  40.             IOUtils.closeQuietly(inputStream);
  41.         }
  42.         return JSON.toJSONString(props);
  43.     }
  44. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 05:05

Copyright 2015-2025 djqfx

返回顶部