代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the shool to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//…
(3)按照上面的打印格式将内容写入到D:\count.txt文件中(要求用高效流)
package zuoye1105;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class Demo07 {
public static void main(String[] args) throws IOException {
String str1="if you want to change your fate i think you must to the shool to learn java";
String[] aa = str1.split(" +");//分割并存入数组
Map<String, Integer> map =new HashMap<String, Integer>();
BufferedWriter bw =new BufferedWriter(new FileWriter("F:\\count.txt"));
for(String a:aa) {
if(map.containsKey(a)) {
map.put(a, map.get(a)+1);
}else {
map.put(a,1);
}
}
for (Map.Entry<String, Integer> me : map.entrySet()) {
System.out.println(me.getKey()+"------"+me.getValue());
bw.write(me.getKey()+"="+me.getValue());
bw.newLine();
bw.flush();
}
bw.close();
}
}