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.createPrettyPrint();// 格式 format.setEncoding("utf-8");// 设置格式编码 try { /** 将document中的内容写入文件中new XMLWriter(new FileWriter(new File(filename))); */ XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)),format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); ret=false; } return ret; } /** * 格式JSON,使Json美化 * @param content * @return */ public static String formatAsJSON(String content){ Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jsonPar = new JsonParser(); JsonElement jsonEl = jsonPar.parse(content); String prettyJson = gson.toJson(jsonEl); return prettyJson; } }
相关推荐
Java读写文本文件的示例代码 1个目标文件。 java俄罗斯方块 一个目标文件。 Java非对称加密源码实例 1个目标文件 摘要:Java源码,算法相关,非对称加密 Java非对称加密源程序代码实例,本例中使用RSA加密技术,...
2.16 以指定列宽格式化字符串 2.17 在字符串中处理html和xml 2.18 字符串令牌解析 2.19 实现一个简单的递归下降分析器 2.20 字节字符串上的字符串操作 第三章:数字日期和时间 3.1 数字的四舍五入 3.2 执行...
Java读写文本文件的示例代码 1个目标文件。 java俄罗斯方块 一个目标文件。 Java非对称加密源码实例 1个目标文件 摘要:Java源码,算法相关,非对称加密 Java非对称加密源程序代码实例,本例中使用RSA加密技术,...
|--格式化之DecimalFormat数字格式化 |--桌面快捷方式的添加代码示例 |--桌面控件widget的创建方法 |--模拟发短信 指定号码 短信窃听提示 |--消息机制 mesage looper |--滑动之左右滑动的两种定义 |--电话簿读取联系...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
Eclipse HTML Tidy 是一款 Eclipse 的插件,用来对 HTML 文件内容进行清洗和格式化处理。它采用的是 JTidy 库对HTML进行处理。 INI文件操作类库 [ini4j] [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置...
AJAX技术还使用了一种不同于XML格式的JSON文件格式,这个意义在哪里呢?在REST软件架构下我们不能对于XML文件进行序列化处理,这样程序员必须要使用自己的XML绑定框架。而以序列化的JavaScript对象为基础的JSON已经...