import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; import com.google.common.io.Resources; /** * properties文件转为json字符串 * * @author James * @version $Id: MyPropToJson.java, v 0.1 2016年8月22日 下午4:17:18 James Exp $ */ public class MyPropToJson { private static final Logger LOGGER = LoggerFactory.getLogger(MyPropToJson.class); /** * * @param args */ public static void main(String[] args) { String resourceName = "config/test.properties"; System.out.println(toJSONString(resourceName)); } /** * properties文件转为json字符串 * * @param resourceName * 例如,properties文件位置 ,"config/xxx.properties" */ public static String toJSONString(String resourceName) { InputStream inputStream = null; Properties props = new Properties(); try { inputStream = Resources.getResource(resourceName).openStream(); props.load(inputStream); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(inputStream); } return JSON.toJSONString(props); } }
开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»
发表评论 回到顶部 网友评论(0)