本文共 842 字,大约阅读时间需要 2 分钟。
题目实现
用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must learn java" (2)打印格式: to=3 think=1 you=2 //........ (3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
代码实现
public class demo { public static void main(String[] args) throws IOException { String string = "If you want to change your fate I think you must learn java"; String[] strings = string.split(" "); Mapmap = new HashMap<>(); for(String s:strings) { int count = 0; if(map.containsKey(s)) { count = map.get(s); } count++; map.put(s,count); } BufferedWriter b = new BufferedWriter(new FileWriter("D:/count.txt")); Set > set = map.entrySet(); for(Entry entry:set) { System.out.println(entry); b.write(entry.getKey()+"="+entry.getValue()); b.newLine(); } b.close(); System.out.println("完成"); }}
转载地址:http://jpazi.baihongyu.com/