赞
踩
package org.util; import org.apache.commons.io.IOUtils; import org.json.JSONObject; import java.io.FileInputStream; import java.io.IOException; public class JsonTool { //读取文件内容并以String类型返回 public String readFileToString(String filePath) { String jsonString = null; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); jsonString = IOUtils.toString(fileInputStream, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return jsonString; } //读取文件内容并以JSONObject类型返回 public JSONObject readFileToObject(String filePath) { String jsonString = readFileToString(filePath); //将String类型的文件内容转为JSONObject类型 JSONObject jsonObject = new JSONObject(jsonString); return jsonObject; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。