课堂训练
Posted quyangzhangsiyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课堂训练相关的知识,希望对你有一定的参考价值。
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 + " 3出现次数:" + hashMap.get(word1));
bw.newLine();
System.out.printf("单词:%-12s 出现次数:%d
",word1,hashMap.get(word1));
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
经过在网上不断的找资料和舍友的帮助,也算是完成了,不会的东西还是很多。
以上是关于课堂训练的主要内容,如果未能解决你的问题,请参考以下文章