package com.org.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; /** *@CopyRight:liangjilong *@DEMO:midu *@Author:jilongliang *@Email:jilongliang@sina.com *@Date:2014-1-25 */ public class HadlerUtil { private static String content = "",line=System.getProperty("line.separator");//换行相当于\n /** * 读文件流 * @param formPath从哪里读取的文件路径 * @return */ public static Map<String, String> readConfig(String formPath) { String content = ""; Map<String, String> map = new HashMap<String, String>(); FileReader read = null; BufferedReader reader = null; try { read = new FileReader(new File(formPath)); reader = new BufferedReader(read); content = reader.readLine(); while (content != null) { String val[] = content.split("="); if (val.length > 0) { String key = val[0];// ,分开 map.put(key, val[1]); } content = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null||read!=null) reader.close(); read.close(); } catch (Exception e) { e.printStackTrace(); } } return map; } /** * 读文件流 * @param formPath从哪里读取的文件路径 * @return */ public static String reader(String formPath) { FileReader read = null; BufferedReader reader = null; try { read = new FileReader(new File(formPath)); reader = new BufferedReader(read); StringBuffer buffer = new StringBuffer(""); content = reader.readLine(); while (content != null) { buffer.append(content).append(line); content = reader.readLine(); } return content = buffer.toString();//返回 } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null)reader.close(); if (read != null)read.close(); } catch (Exception e) { e.printStackTrace(); } } return "";//没值就返回空 } /** * 文件处理 * @param content * @param toPath写到那个路径下 * @return */ public static boolean writer(String content, String toPath) { boolean flag = true; try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toPath), "utf-8"));//设置编码 out.write("\n" + content); out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * xml格式化,使用xml数据美化 * @param document * @param filePath * @return */ public static boolean formatAsXml(Document document) { boolean ret=true; OutputFormat format = OutputFormat.createPrettyPrint();// 格式 format.setEncoding("utf-8");// 设置格式编码 try { /** 将document中的内容写入文件中new XMLWriter(new FileWriter(new File(filename))); */ XMLWriter writer = new XMLWriter(format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); ret=false; } return ret; } /** * xml格式化,使用xml数据美化 * @param document * @param filePath * @return */ public static boolean formatAsXml(Document document, String filePath) { boolean ret=true; OutputFormat format = OutputFormat.c