求助,麻烦大家了.题目要求使用HASHTABLE和LINKEDLIST建立一个字典,把结果保存在一个文件(TXT)上,然后每次开始执行程序时读出文件里的数据
import java.io.*;
public class Dufwords //DOUBOULE LINKEDLIST
{
public class wordNode implements Serializable
{
public String key;
public String meaning;
public transient wordNode previous;
public transient wordNode next;
}
public transient wordNode first;
public transient wordNode currentPos;
public transient wordNode last;
public transient int numItems;
public Dufwords()
{
numItems=0;
first=null;
currentPos=null;
last=null;
}
public int Amount()
{
return numItems;
}
public void Addend(String w,String m)
{
wordNode newNode=new wordNode();
if(first==null)
{
first=newNode;
last=newNode;
newNode.key=w;
newNode.meaning=m;
}
else
{
newNode.key=w;
newNode.meaning=m;
newNode.previous=last;
last.next=newNode;
last=newNode;
newNode.next=null;
}
numItems++;
}
public void resetforward()
{
currentPos=first;
}
public boolean isThere(String s)
{
currentPos=first;
boolean found=false;
while(!found&¤tPos!=null)
{
if(currentPos.key.compareToIgnoreCase(s)==0)
{
found=true;
}
else
{
currentPos=currentPos.next;
}
}
return found;
}
public wordNode toFind(String s)
{
currentPos=first;
boolean found=false;
while(!found&¤tPos!=null)
{
if(currentPos.key.compareToIgnoreCase(s)==0)
{
found=true;
}
else
{
currentPos=currentPos.next;
}
}
return currentPos;
}
public void toDlete(String s)
{
wordNode tD=toFind(s);
if(tD==first&&tD.next==null)
{
first=null;
last=null;
}
else if(tD==first&&tD.next!=null)
{
first=tD.next;
tD.next.previous=null;
tD=null;
}
else if(tD!=first&&tD==last)
{
tD.previous.next=null;
last=tD.previous;
tD.previous=null;
tD=null;
}
else
{
tD.previous.next=tD.next;
tD.next.previous=tD.previous;
}
}
}
import java.io.*;
import java.lang.*;
public class dictionary
{
public static void main(String args[])
{
Dufwords[] dic;
dic=new Dufwords[100];
while(true)
{
System.out.println("Please enter the word ,use Enter then you can choose the thing u want to do.");
String words=Keyboard.readString();
int wor=words.hashCode();
String re=""+wor;
int resault=Integer.parseInt(re.substring(re.length()-2));
System.out.println("Please choose the function:1.add new word. 2.Delete a word. 3.Update a word. 4.look up the definition fo word.");
int choose=Keyboard.readInt();
switch(choose)
{
case 1:
{
if(dic[resault]==null)
{
System.out.println("Please enter the meaning of word.");
String t=Keyboard.readString();
Dufwords newDlist=new Dufwords();
dic[resault]=newDlist;
newDlist.Addend(words,t);
}
else
{
if(dic[resault].isThere(words))
{
System.out.println("the word has existed.");
System.exit(1);
}
else
{
System.out.println("Please enter the meaning of word.");
String t=Keyboard.readString();
dic[resault].Addend(words,t);
}
}
break;
}//end of case 1
case 2:
{
if(dic[resault]==null)
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
else
{
if(dic[resault].isThere(words))
{
dic[resault].toDlete(words);
}
else
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
}
break;
}//end of case 2
case 3:
{
if(dic[resault]==null)
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
else
{
if(dic[resault].isThere(words))
{
System.out.println("Please update the meaning.");
String update=Keyboard.readString();
dic[resault].toFind(words).meaning=update;
}
else
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
}
break;
}//end of case 3
case 4:
{
if(dic[resault]==null)
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
else
{
if(dic[resault].isThere(words))
{
System.out.println(dic[resault].toFind(words).meaning);
}
else
{
System.out.println("The word doesn't exist.");
System.exit(1);
}
}
break;
}//end of case 4
}//end of switch
try{
File outFile=new File("c:\\2529279.txt");
FileOutputStream outFileStream=new FileOutputStream(outFile);
ObjectOutputStream outObjectStream=new ObjectOutputStream(outFileStream);
outObjectStream.writeObject(dic[resault].last);
}catch(Exception e){
System.out.println("error.");
}
}//end of while
}//end of main
}//end of class
我刚学JAVA没多久,对I/O很不熟悉,,我现在想把Dufwords的内部类中的KEY 和MEANING存入文件
然后程序重新执行时将数据读出,放进链表
哪位大大有空帮忙看一下,给点提示如何使用I/O,程序写得有点乱,麻烦大家了,非常感谢.
求助,关于JAVA的I/O系统