import java.io.BufferedReader;
import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Scanner;import java.util.Set;public class Text{
public static void main(String [] args) { File word=new File("G:\\JAVA\\新建文件夹\\word\\word.txt"); File tongji=new File("G:\\JAVA\\新建文件夹\\word\\tongji.txt"); try { BufferedReader read=new BufferedReader(new FileReader(word)); StringBuffer d=new StringBuffer(); String s=null; while((s=read.readLine())!=null) { d.append(s); } read.close(); Scanner scanner=new Scanner(word); HashMap<String, Integer > hashMap=new HashMap<String,Integer>(); while(scanner.hasNextLine()) { String line=scanner.nextLine(); System.out.println(line); String[] lineWords=line.split("\\W+"); Set<String> wordSet=hashMap.keySet(); for(int i=0;i<lineWords.length;i++) { if(wordSet.contains(lineWords[i])) { Integer number=hashMap.get(lineWords[i]); number++; hashMap.put(lineWords[i], number); } else { hashMap.put(lineWords[i], 1); } }}
Iterator<String> iterator=hashMap.keySet().iterator(); BufferedWriter bw = new BufferedWriter(new FileWriter("G:\\tongji.txt")); while(iterator.hasNext()) { String word1=iterator.next(); bw.write("单词:"+ word1 + "\t3出现次数:" + hashMap.get(word1)); bw.newLine(); System.out.printf("单词:%-12s 出现次数:%d\n",word1,hashMap.get(word1)); } bw.close(); } catch (IOException e) { e.printStackTrace(); }}}
经过在网上不断的找资料和舍友的帮助,也算是完成了,不会的东西还是很多。