public class IoTest02 {
public static void main(String[] args) throws IOException {
writelist ();
writeMap ();
}
@SuppressWarnings("unused")
private static void writeMap() throws IOException {
Map<String, String> map = new HashMap<String, String> ();
map.put("琦玉", "龙卷");
map.put("路飞", "女帝");
map.put("鸣人", "雏田");
map.put("柯南", "灰原哀");
@SuppressWarnings("resource")
BufferedWriter bw = new BufferedWriter(new FileWriter("q.txt",true));
Set<Entry<String, String>> keyValues = map.entrySet();
StringBuffer sb = new StringBuffer();
for (Entry<String, String> entry : keyValues) {
sb.delete(0, sb.length());
String key = entry.getKey();
String value = entry.getValue();
sb.append(key).append("=").append(value);
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
private static void writelist() throws IOException {
List<String> list = new ArrayList<String> ();
list.add("a");
list.add("b");
list.add("c");
BufferedWriter bw = new BufferedWriter(new FileWriter("q.txt"));
for (String string : list) {
bw.write(string);
bw.newLine();
bw.flush();
}
bw.close();
}
}
public class IoTest03 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("q.txt"));
List<String> list = new ArrayList<String> ();
Map<String, String> map = new HashMap<String, String> ();
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains("=")) {
String[] str = line.split("=");
map.put(str[0], str[1]);
} else {
list.add(line);
}
}
br.close();
for (String string : list) {
System.out.println(string);
}
Set<String> set = map.keySet();
for (String string : set) {
String value = map.get(string);
System.out.println(string + "=" + value);
}
}
}