读取linux服务器内带格式文件,转为json字符串
工具类方法:ReadTextUtil
1 package com.dc.health.platform.common.utils; 2 3 import com.alibaba.fastjson.JSONObject; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 public class ReadTextUtil { 9 10 public static JSONObject ReadTextToJson(String path) { 11 if (path == null || "".equals(path)) 12 return null; 13 List<JSONObject> jsObjectList = new ArrayList<>(); 14 List<String> flist = SSHUtil.execCom("cat " + path); 15 if (flist != null && flist.size() > 0){ 16 for (String s : flist) { 17 JSONObject jsObject = new JSONObject(); 18 String[] str = s.split("\\s+"); 19 if (str != null && str.length > 1){ 20 jsObject.put(str[0], str[1]); 21 }else if (str != null && str.length > 0){ 22 jsObject.put(str[0], ""); 23 } 24 jsObjectList.add(jsObject); 25 } 26 } 27 JSONObject json = new JSONObject(); 28 json.put("data", jsObjectList); 29 return json; 30 } 31 }
链接linux服务器:http://www.cnblogs.com/smbin/p/7736896.html