java实现从文件中读取信息保存在Map中,修改Map中的值,将Map 4000 更新到文件中
2017-05-24 10:23
537 查看
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”;// 文件相对路径
}
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; }
}
相关文章推荐
- java实现从文件中读取信息保存在Map中,修改Map中的值,将Map更新到文件中
- Java Properties 类读取和修改配置文件信息
- java实现读取文件底下所有子文件名,并修改文件名
- 使用JAVA实现比较两个文件夹下的文件新增和修改情况,并复制到新的目录(实现增量更新项目)
- 用java实现print screen,读取位图文件,将位图文件转化位jpeg格式并保存成文件输出!
- JMF实现将从摄像头和麦克读取的媒体信息保存成视频文件
- Java实现文件的读取、写入、复制、修改、移动等操作
- (转)实现对Java配置文件Properties的读取、写入与更新操作
- Java实现读取键盘输入保存到txt文件,再统计并输出每个单词出现次数的方法
- java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)
- 实现学生信息的文件保存和读取
- Java Properties 类读取和修改配置文件信息
- Java Properties 类读取和修改配置文件信息
- Java实现的properties文件动态修改并自动保存工具类
- Java Properties文件的读取和修改保存
- xml文件的读取修改(写回原文件,也就是能保存更新)
- java实现保存文件图片以及读取
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息