import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Demo13 {
/**
* * 在当前项目工程下有个data.txt文件(该文件由自己创建,数据在右侧,可复制),
* 现要求使用map集合将上面按姓名自然排序,将”郭美美”年龄修改为”19”,删除”房祖名”这组数据,
* 然后对应输出到D盘(没有D盘就使用其他盘)根目录下的num.txt中,
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TreeMap<String, Integer> tm = new TreeMap<>();
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
String len;
while ((len = br.readLine()) != null) {
String[] arr = len.split("=");
tm.put(arr[0], Integer.parseInt(arr[1]));
}
br.close();
tm.remove("房祖名");
for (String s : tm.keySet()) {
if ("郭美美".equals(s)) {
tm.put(s, 19);
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d:/num.txt")));
for (Entry<String, Integer> en : tm.entrySet()) {
bw.write(en.getKey() + "=" + en.getValue());
bw.newLine();
}
bw.close();
}
}
|
|