java实现从文件中读取信息保存在Map中,修改Map中的值,将Map更新到文件中
来源:互联网 发布:zip源码 编辑:程序博客网 时间:2024/04/26 00:53
package com.filetomap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 创建一个file,后缀名为cfg. 里面文本格式为每行:A=X结构,其中注释在最前面加上# 然后将该文件保存到map中(map(A,X);
*
* @author Administrator
*
*/
public class FileToMap1 {
private String line = System.getProperty(“line.separator”);
private String separ = “=”;
static String basePath = System.getProperty(“user.dir”);
static String filePath = “./Alms.cfg”;// 文件相对路径
public static void main(String[] args) { FileToMap1 fileToMap1 = new FileToMap1(); Map<String, String> oldMap = fileToMap1.fTM(); Map<String, String> newMap = new HashMap<String, String>(); newMap.put("A", "A''"); Map<String, String> map = fileToMap1.newMapToOldMap(newMap, oldMap, false); try { fileToMap1.mapToFileDefault(map, new File(filePath), fileToMap1.separ); } catch (IOException e) { e.printStackTrace(); }}/** * 有参构造函数 * * @param separ * @param filePath */public FileToMap1(String separ, String filePath) { this.filePath = filePath; this.separ = separ; File file = new File(filePath); if (!file.exists()) { System.out.println("文件不存在"); try { file.createNewFile(); } catch (IOException e) { System.out.println("路径:" + filePath + "创建失败"); e.printStackTrace(); } }}/** * 无参构造函数,用类默认的配置。 */public FileToMap1() { File file = new File(filePath); if (!file.exists()) { System.out.println("文件不存在"); try { file.createNewFile(); } catch (IOException e) { System.out.println("路径:" + filePath + "创建失败"); e.printStackTrace(); } }}/** * 将map写入到file文件中。默认map(String A,String A')file中以A=A'来表示,map中每个键值对显示一行 * @throws IOException */private File mapToFileDefault(Map<String, String> map,File file,String separ) throws IOException{ StringBuffer buffer = new StringBuffer(); FileWriter writer = new FileWriter(file, false); for(Map.Entry entry:map.entrySet()){ String key = (String) entry.getKey(); String value = (String) entry.getValue(); buffer.append(key + "=" + value).append(line); } writer.write(buffer.toString()); writer.close(); return file;}/** * 在newMap替换oldMap时,是否覆盖(isOverwrite)如果是,就直接替换,如果否,则将oldMap中的key前加“#”,默认为否 * @param newMap * @param oldMap * @return */private Map<String, String> newMapToOldMapDefault(Map<String, String> newMap,Map<String, String> oldMap){ return newMapToOldMap(newMap,oldMap,false);}/** * 在newMap替换oldMap时,是否覆盖(isOverwrite)如果是,就直接替换,如果否,则将oldMap中的key前加“#”,默认为否 */private Map<String, String> newMapToOldMap(Map<String, String> newMap, Map<String, String> oldMap, boolean isOverwrite) { // 由于oldMap中包含了file中更多内容,所以newMap中内容在oldMap中调整后,最后返回oldMap修改之后的map. // 如果选择true覆盖相同的key if (isOverwrite) { // 循环遍历newMap for (Map.Entry entry : newMap.entrySet()) { String newKey = (String) entry.getKey(); String newValue = (String) entry.getValue(); oldMap.put(newKey, newValue); } } else { // 不覆盖oldMap,需要在key相同的oldMap的key前加#; // 循环遍历newMap for (Map.Entry entry : newMap.entrySet()) { String newKey = (String) entry.getKey(); String newValue = (String) entry.getValue(); String oldValue = oldMap.get(newKey); oldMap.put("#" + newKey, oldValue); oldMap.put(newKey, newValue); } } return oldMap;}/** * 将文件转换成map存储 * * @return */private Map<String, String> fTM() { Map<String, String> map = new HashMap<String, String>(); File file = new File(filePath); BufferedReader reader = null; try { System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); if (!tempString.startsWith("#")) { String[] strArray = tempString.split("="); map.put(strArray[0], strArray[1]); } line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } return map;}
}
1 0
- java实现从文件中读取信息保存在Map中,修改Map中的值,将Map更新到文件中
- java实现从文件中读取信息保存在Map中,修改Map中的值,将Map更新到文件中
- android 将map保存到sqlite数据库中,从表数据中取回map
- java中读取properties文件,并将文件内容转换成map并返回
- Java将对象保存到文件中/从文件中读取对象
- sharedPreference存储对象-数据库存储对象;android 将map保存到sqlite数据库中,从表数据中取回map
- 如何在map文件中生成行信息?
- 如何在map文件中生成行信息?
- 利用java反射将map值封装到对象中
- Java List<map<String,object>> 放在文件 txt 中
- 在js中模拟Java中的Map
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- 在 Map[Reduce] 的 setup 中读取 HDFS 文件夹信息
- CCS中MAP文件说明
- 【redis,1】java操作redis: 将string、list、map、自定义的对象保存到redis中
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- java操作redis: 将string、list、map、自定义的对象保存到redis中
- poj-3080 Blue Jeans
- Select2插件 IE下 autofocus bug的解决方法
- Struts2 基础总结
- 学习Javascript闭包(Closure)
- TableViewCell中cell高度不一样时的优化
- java实现从文件中读取信息保存在Map中,修改Map中的值,将Map更新到文件中
- Eclipse中Android源代码新下载方法及关联
- 数据结构(2)基本排序算法
- Spring异步任务处理,@Async的配置和使用
- 菜鸟系列——置换群
- Java String在内存中的情况
- HDU1513:Palindrome 【LCS】
- 创建BeatBox
- 如何打造高效的团队?