📄 exercise18_2.java
字号:
import java.util.*;
import java.io.*;
public class Exercise18_2 {
public static void main(String[] args) throws IOException {
System.out.println("写入文本文件中的内容为:");
String text = "";
FileWriter output=new FileWriter("temp.txt",false);//创建一个FileWriter类的对象output,并且不可再更改
output.write("wang yong chao zhang xin ying, Welcome to Java ! ");//写入文本文件中的内容
output.close(); //关闭文本文件
FileReader input=new FileReader("temp.txt");//创建一个FileReader类的对象input,将文本文件中的内容读出来
int code;
while((code=input.read())!=-1){
text+=(char)code;//text中保存文本文件中的内容
}
input.close();
System.out.println(text);
Map<String, Integer> hashMap = new HashMap<String, Integer>();//创建一个Map类的对象hashMap设置主关键字的类型为String
StringTokenizer st = new StringTokenizer(text, " .!?");//当遇到" .!?"时进行截取
while (st.hasMoreTokens()) {
String key = st.nextToken();
if (hashMap.get(key) != null) {
int value = hashMap.get(key).intValue();
value++;
hashMap.put(key, value);
}
else {
hashMap.put(key, 1);
}
}
Map<String, Integer> treeMap =new TreeMap<String, Integer>(hashMap);
System.out.println("各个单词按升序不重复的输出为:");
System.out.print(treeMap);
System.out.println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -