|
| 1 | +package com.xiaour.spring.boot.utils; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.FileWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import org.apache.http.util.TextUtils; |
| 15 | + |
| 16 | +import com.alibaba.fastjson.JSON; |
| 17 | +import com.alibaba.fastjson.JSONObject; |
| 18 | + |
| 19 | +public class FileUtils { |
| 20 | + public static final String FILEPATH = "Permanent_Data"; |
| 21 | + |
| 22 | + // json写入文件 |
| 23 | + public synchronized static void write2File(Object json, String fileName) { |
| 24 | + BufferedWriter writer = null; |
| 25 | + File filePath = new File(FILEPATH); |
| 26 | + JSONObject eJSON = null; |
| 27 | + |
| 28 | + if (!filePath.exists() && !filePath.isDirectory()) { |
| 29 | + filePath.mkdirs(); |
| 30 | + } |
| 31 | + |
| 32 | + File file = new File(FILEPATH + File.separator + fileName + ".xml"); |
| 33 | + System.out.println("path:" + file.getPath() + " abs path:" + file.getAbsolutePath()); |
| 34 | + if (!file.exists()) { |
| 35 | + try { |
| 36 | + file.createNewFile(); |
| 37 | + } catch (Exception e) { |
| 38 | + System.out.println("createNewFile,出现异常:"); |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + } else { |
| 42 | + eJSON = (JSONObject) read2JSON(fileName); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + writer = new BufferedWriter(new FileWriter(file)); |
| 47 | + |
| 48 | + if (eJSON==null) { |
| 49 | + writer.write(json.toString()); |
| 50 | + } else { |
| 51 | + Object[] array = ((JSONObject) json).keySet().toArray(); |
| 52 | + for(int i=0;i<array.length;i++){ |
| 53 | + eJSON.put(array[i].toString(), ((JSONObject) json).get(array[i].toString())); |
| 54 | + } |
| 55 | + |
| 56 | + writer.write(eJSON.toString()); |
| 57 | + } |
| 58 | + |
| 59 | + } catch (IOException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } finally { |
| 62 | + try { |
| 63 | + if (writer != null) { |
| 64 | + writer.close(); |
| 65 | + } |
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + // 读文件到json |
| 74 | + public static JSONObject read2JSON(String fileName) { |
| 75 | + File file = new File(FILEPATH + File.separator + fileName + ".xml"); |
| 76 | + if (!file.exists()) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + BufferedReader reader = null; |
| 81 | + String laststr = ""; |
| 82 | + try { |
| 83 | + reader = new BufferedReader(new FileReader(file)); |
| 84 | + String tempString = null; |
| 85 | + while ((tempString = reader.readLine()) != null) { |
| 86 | + laststr += tempString; |
| 87 | + } |
| 88 | + reader.close(); |
| 89 | + } catch (IOException e) { |
| 90 | + e.printStackTrace(); |
| 91 | + } |
| 92 | + |
| 93 | + return (JSONObject) JSON.parse(laststr); |
| 94 | + } |
| 95 | + |
| 96 | + // 通过key值获取文件中的value |
| 97 | + public static Object getValue(String fileName, String key) { |
| 98 | + JSONObject eJSON = null; |
| 99 | + eJSON = (JSONObject) read2JSON(fileName); |
| 100 | + if (null != eJSON && eJSON.containsKey(key)) { |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + Map<String, Object> values = JSON.parseObject(eJSON.toString(), Map.class); |
| 103 | + return values.get(key); |
| 104 | + } else { |
| 105 | + return null; |
| 106 | + } |
| 107 | + } |
| 108 | + public static HashMap<Long, Long> toHashMap(JSONObject js) |
| 109 | + { |
| 110 | + if(js == null){ |
| 111 | + return null; |
| 112 | + } |
| 113 | + HashMap<Long, Long> data = new HashMap<Long, Long>(); |
| 114 | + // 将json字符串转换成jsonObject |
| 115 | + Set<String> set = js.keySet(); |
| 116 | + // 遍历jsonObject数据,添加到Map对象 |
| 117 | + Iterator<String> it = set.iterator(); |
| 118 | + while (it.hasNext()) |
| 119 | + { |
| 120 | + String key = String.valueOf(it.next()); |
| 121 | + Long keyLong = Long.valueOf(key); |
| 122 | + |
| 123 | + String value = js.getString(key); |
| 124 | + Long valueLong; |
| 125 | + if(TextUtils.isEmpty(value)){ |
| 126 | + valueLong = js.getLong(key); |
| 127 | + }else{ |
| 128 | + valueLong = Long.valueOf(value); |
| 129 | + } |
| 130 | + data.put(keyLong, valueLong); |
| 131 | + } |
| 132 | + return data; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +} |
0 commit comments