exercise18_2.java

来自「一款用java编写的小型数据库管理系统」· Java 代码 · 共 37 行

JAVA
37
字号
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 + =
减小字号Ctrl + -
显示快捷键?