| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 542 人关注过本帖
标题:求助,关于JAVA的I/O系统
只看楼主 加入收藏
qijiayan7
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-10-30
收藏
 问题点数:0 回复次数:1 
求助,关于JAVA的I/O系统

求助,麻烦大家了.题目要求使用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&&currentPos!=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&&currentPos!=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 系统 
2005-10-30 21:13
qijiayan7
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-10-30
收藏
得分:0 

我想把整个对象读入文件,不必要的参数用transient防止串行化,但是老是出乱码,有人能给点建议吗?麻烦了.

2005-10-31 03:34
快速回复:求助,关于JAVA的I/O系统
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036426 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved