用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
public class Demo22 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String str = "If you want to change your fate"
+ " I think you must come to the dark horse to learn java";
printStr(str);
}
private static void printStr(String str) throws IOException {
// TODO Auto-generated method stub
HashMap<String, Integer> map = new HashMap<>();
String [] strs = str.split(" ");
for(String s :strs){
if(map.containsKey(s)){
map.put(s, map.get(s)+1);
}else{
map.put(s, 1);
}
}
System.out.println(map);
File file = new File("D:\\count.txt");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
for(String s : map.keySet()){
String ss = s+"="+map.get(s);
System.out.println(ss);
osw.write(ss+'\n');
}
osw.close();
}
}