您的位置:首页 > 其它

map存储到文件

2017-11-24 11:57 337 查看
/** 

* 创建一个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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: