帮我做个JAVA题

Java程序设计》课程设计题目说明 1、题目:简单的英文词处理器2、实验目的:(1)学习应用Collection Framework 存储和处理数据。(2)掌握Java I/O流操作。(3)学会正确应用Java的异常处理机制。(4)掌握应用Comparable和Comparator排序。(5)熟悉面向对象的编程,体会继承和重载的作用。3、主要功能:读入文件中的英文单词,将数据存入Collection Framework的集合类中,对单词分别以字母顺序、出现频率和单词长度进行排序,输出排序结果,并将排序后数据存入文件。4、平台:(1)Java语言(2)JDK1.5(3)NetBean5.55、设计参数:(1)建立文本文件,设计阅读器,从文件中读入单词。(2)实现排序器,以满足特定排序规则的排序方式。(3)正确输出数据到文件。6、设计提示:阅读The Java Tutorial –collections专题。参考程序:教案Chapter10-code10。
谢谢,朋友求的,晚上要

先给你看下下面的程序的运行结果!!单词:academic 出现次数:1 单词长度:8
单词:and 出现次数:1 单词长度:3
单词:applies 出现次数:1 单词长度:7
单词:correctly 出现次数:1 单词长度:9
单词:exception 出现次数:1 单词长度:9
单词:frequency 出现次数:1 单词长度:9
单词:grasps 出现次数:1 单词长度:6
单词:handling 出现次数:1 单词长度:8
单词:mechanism 出现次数:1 单词长度:9
单词:society 出现次数:1 单词长度:7 下面是程序的代码:一共有3个类,分别是:Word.java(模型层) FileService.java(业务层) Test.java(视图层) /************************************************************************************************/package question3.model;import java.io.Serializable;/**
* 实体类Word
* @author admin
*
*/
public class Word implements Serializable,Comparable<Word>{

private String value;//保存单字的值
private Integer frequency ;//单词出现的频率
private Integer length;//单词的长度

/**
* 对单词分别以字母顺序、出现频率和单词长度进行排序
*/
@Override
public int compareTo(Word word) {
//先比较字母的顺序
if(this.value.hashCode()!=word.getValue().hashCode()){
//区分大小写
return this.value.trim().compareTo(word.getValue().trim());
// //不区分大小写
// return this.value.trim().compareToIgnoreCase(word.getValue().trim());
}
//比较出现的频率
if(this.frequency!=word.getFrequency()){
return this.frequency-word.getFrequency();
}
//比较长度
if(this.length!=word.getLength()){
return this.length-word.getLength();
}
return 0;
}

public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Integer getFrequency() {
return frequency;
}
public void setFrequency(Integer frequency) {
this.frequency = frequency;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
}
/*****************************************************************************************************************/ package question3.service;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.TreeSet;import question3.model.Word;public class FileService { /**
* 读取文件
* @param filePath
* @return
*/
public static String readFile(String filePath){
File file=null;
byte[] content=null;
Integer length=null;
BufferedInputStream bis=null;
file=new File(filePath);
try {
bis=new BufferedInputStream(new FileInputStream(file));
length=bis.available();
content=new byte[length];
bis.read(content, 0, length);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bis!=null)try{bis.close();}catch(Exception e1){}
}
return new String(content);
}

/**
* 将字符串中的单词根据空格读取出来,并保存到集合中
* @param content
* @return
*/
public Collection<Word> handleString(String content){
Collection<Word> wordCol=new TreeSet<Word>();
String[] strArray=content.split(" ");
for(String s:strArray){
if(s!=null && s.trim().length()>0){
Word exitedWord=isWordExisted(wordCol, content);
//不存在
if(exitedWord==null){
Word word=new Word();
word.setFrequency(1);
word.setLength(s.trim().length());
word.setValue(s.trim());
wordCol.add(word);
}else{//存在
exitedWord.setFrequency(exitedWord.getFrequency()+1);
wordCol.add(exitedWord);
}
}
}
return wordCol;
}

/**
* 判断该单词是否在集合中存在
* @param wordCol
* @param wordValue
* @return
*/
private Word isWordExisted(Collection<Word> wordCol,String wordValue){
for(Word word:wordCol){
if(word.getValue().trim().equals(wordValue.trim())){
return word;
}
}
return null;
}

/**
* 查看集合中的单词
* @param wordColl
*/
public void show(Collection<Word> wordCol){
for(Word word:wordCol){
System.out.println("单词:"+word.getValue()+"\t出现次数:"+word.getFrequency()+"\t单词长度:"+word.getLength());
}
}

/**
* 将集合中的单词保存的文件中
* @param wordCol
* @param filePath
*/
public void storeWords(Collection<Word> wordCol,String filePath){
String content=escapeValue(wordCol);
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(new File(filePath)));
oos.write(content.getBytes());
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(oos!=null)
try{oos.close();}catch(Exception e){}
}
}

/**
* 将集合中的单词拼成一个字符串
* @param wordCol
* @return
*/
private String escapeValue(Collection<Word> wordCol){
StringBuffer buffer=new StringBuffer();
for(Word word:wordCol){
if(word!=null && word.getValue()!=null && word.getValue().trim().length()>0){
buffer.append(" "+word.getValue());
}
}
return buffer.toString();
}

}
/**************************************************************************************************************/ package question3.client;import java.util.Collection;import question3.model.Word;
import question3.service.FileService;public class Test {
public static void main(String[] args){
FileService fs=new FileService();
String content=fs.readFile("input.txt");//从改文件中读取单词
Collection<Word> wordCol=fs.handleString(content);
fs.show(wordCol);
fs.storeWords(wordCol, "db.txt");//将单词存在到该文件中
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-01-21
哎。。悲惨啊。 自己看看API写吧。
第2个回答  2014-01-21
import java.io.*;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;public class fileRead { /**
* @param args
* @author lgq
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String data = null;
List<Map.Entry<Integer, String>> list = new
ArrayList<Map.Entry<Integer, String>>(); //保存单词list

int count = 0;
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream("d:\\test.txt")));

while((data = br.readLine())!=null)
{
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(count, data);
list.addAll(map.entrySet());

System.out.println("head "+data);
count++;
}
}catch(IOException ioe)
{
ioe.printStackTrace();
}
Collections.sort(list, new Comparator<Map.Entry<Integer, String>>()
{
public int compare(Map.Entry<Integer, String> o1,
Map.Entry<Integer, String> o2) {
// TODO Auto-generated method stub
//return (o2.getValue() - o1.getValue());
//return (o1.getKey()).toString().compareTo(o2.getKey().toString());
return(o1.getValue().compareTo(o2.getValue()));
}
});
//写入 文本
try
{
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("d:\\test.txt"));

for(int i=0;i<list.size();i++)
{
writer.write(list.get(i).toString());

writer.flush();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("d:\\test.txt")),true); pw.println(list.get(i).toString());
}
writer.close();
}catch(IOException io)
{
io.printStackTrace();
}

for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).toString());
}
}}
好了 试下 你,抽了 几个工作时,呵呵,,希望帮到你~~~~
相似回答
大家正在搜