|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先说优点,首先和C,C++这些语言比起来,java很简单,去掉指针的java,非常好理解,自动垃圾回收机制也很好,自从JDK1.5推出以后,性能上又有了很大提高。源代码importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.io.StringReader;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Collections;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.StringTokenizer;
/*
*Createdon2005-1-11
*/
/**
*@author让炜
*@since1.0
*
*TODO先生成就办理体系
*经由过程学号查找,修正,删除数据
*
*/
publicclassLittleProgram
{
staticbooleanisDelete=true;
staticbooleanisFind=true;
publicstaticvoidmain(String[]args)//主办法,程序从这里入手下手运转
throwsIOException,NumberNotFoundException
{
intchoice=-1;
do{
LittleProgramlp=newLittleProgram();
System.out.println();
System.out.println(" ####################################");
System.out.println();
System.out.println(" Java先生成就办理体系1.1");
System.out.println(" 请用学号查找,修正,删除数据");
System.out.println();
System.out.println(" ####################################
");
System.out.print("1.增添数据:
"+
"2.查找数据:
"+
"3.删除数据:
"+
"4.扫除一切数据:
"+
"5.把数据全体打印到屏幕
"+
"6.把成就按学号排序
"+
"7.修正数据
"+
"8.统计已纪录成就先生数
"+
"9.关于作者
"+
"0.加入程序.
"+
"输出:");
BufferedReaderin=//从终
newBufferedReader(//端接
newInputStreamReader(System.in));//收数
StringinputLine=in.readLine();//字选
choice=Integer.valueOf(inputLine).intValue();//项;
switch(choice)
{
case1:{//1.增添数据
Stringstr=lp.inputData();
lp.addData(str);
System.out.println("增添数据乐成.");
timeOut(1);
}break;
case2:{//2.查找数据
longfind=0;
System.out.print("请输出你要查找的先生学号:");
BufferedReaderinn=
newBufferedReader(
newInputStreamReader(System.in));
StringinputLi=inn.readLine();
find=Integer.valueOf(inputLi).longValue();
lp.findData(find);
timeOut(2);
}break;
case3:{//3.删除数据
longdeleteNumber=0;
System.out.print("请输出你想删除的同砚的学号:");
BufferedReaderbf=
newBufferedReader(
newInputStreamReader(System.in));
StringinputL=bf.readLine();
deleteNumber=Integer.valueOf(inputL).longValue();
lp.deleteData(deleteNumber);
if(isDelete)
System.out.println("删除数据乐成!");
timeOut(1);
}break;
case4:{
lp.clearData();//4.扫除一切数据
timeOut(1);
}break;
case5:{
print();//5.把数据全体打印到屏幕
timeOut(2);
}break;
case6:{
lp.numSort();//6.把成就按学号排序
System.out.println("依照学号从小到年夜排序乐成!
"+
"排序后:
");
print();
timeOut(2);
}break;
case7:{
lp.rewrite();//7.修正数据
timeOut(2);
}break;
case8:{
intcount=lp.count();
System.out.println("共有"+count+"个先生已纪录.");
timeOut(2);
}break;
case9:{
System.out.print(" 让炜
"+
" 上海电力学院通讯工程系
"+
" QQ:254482170
");
timeOut(4);
}break;
}while(choice!=0);
System.out.println("Bye!^-^");
System.exit(0);
}
publicStringinputData()//从终端吸收数据的办法,前往字符串
throwsIOException,NumberFormatException
{
System.out.print("请顺次输出:学号姓名性别成就
"+
"每项数据请用空格离隔:");
Stringall="";
try{
BufferedReaderin=//从终
newBufferedReader(//端接
newInputStreamReader(System.in));//收数
StringinputLine=in.readLine();//据
StringTokenizerstr=
newStringTokenizer(inputLine,"");//吸收的数据用空格离隔,这个类用来提取每一个字符串
longnum=Integer.valueOf(str.nextToken()).longValue();//学号
Stringname=(String)str.nextToken();//姓名
Stringsex=(String)str.nextToken();//性别
doublemark=Integer.valueOf(str.nextToken()).doubleValue();//分数
all=String.valueOf(num)+","+
name+","+
sex+","+
String.valueOf(mark);//把一切的数据用","离隔然后在连起来放进字符串all
}catch(IOExceptione){}
catch(NumberFormatExceptione){}
returnall;//前往字符串all
}
publicvoidaddData(Stringstr)//增添数据的办法
throwsIOException
{
Strings1="",s2="",s3="";
Filefile=newFile("data.txt");
if(file.exists())//假如文件data.txt存在
{
try{
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
while((s1=in.readLine())!=null)
s2+=s1+"
";//把文件中的每行数据全体放进一个字符串s2
s2+=str+"
";//再把s2于形参str相连放进s2
BufferedReaderin2=//把字符
newBufferedReader(//串s2也
newStringReader(s2));//就是原
PrintWriterout=//文件+
newPrintWriter(//形参str(新输出的一行数据)
newBufferedWriter(//从头写进data.txt
newFileWriter("data.txt")));//掩盖本来的数据
while((s3=in2.readLine())!=null)
{
out.println(s3);
}
out.close();
//System.out.println("writedatatrue.");
}catch(IOExceptione){}
}else{
System.err.println("File"data"Missing!");
}
}
publicvoidclearData()//扫除data.txt的一切数据的办法
throwsIOException
{
Filefile=newFile("data.txt");
if(file.exists())//假如文件在
{
try{
PrintWriterout=
newPrintWriter(
newBufferedWriter(
newFileWriter(file)));
out.print("");//在文件data.txt里写进一个空字符,以是扫除了本来的内容
out.close();//封闭文件
System.out.println("cleardatatrue!");
}catch(IOExceptione){}
}else{
System.err.println("File"data"Missing!");
}
}
publicvoiddeleteData(longdeleteNumber)//删除某条数据
throwsIOException,FileNotFoundException
{
isDelete=true;
try{
DataMapmp=newDataMap();//天生一个本人编写的容器
longj=0;
Strings1="",s2="",s3="";
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
while((s1=in.readLine())!=null)
{
j=numberTokenizer(s1);
mp.put(j,s1);
}
try{
if(mp.containsKey(String.valueOf(deleteNumber).toString()))
{
mp.remove(deleteNumber);
}else
thrownewNumberNotFoundException();
Collectionc=mp.values();
Iteratoriter=c.iterator();
while(iter.hasNext())
{
s1=(String)iter.next();
s3+=s1+"
";
}
BufferedReaderin2=
newBufferedReader(
newStringReader(s3));
PrintWriterout=
newPrintWriter(
newBufferedWriter(
newFileWriter("data.txt")));
//System.out.println("deleteNo"+deleteNumber);
while((s1=in2.readLine())!=null)
{
out.println(s1);
}
out.close();
}catch(NumberNotFoundExceptione)
{
isDelete=false;
System.out.println(deleteNumber+"nofound:(");
}
}catch(IOExceptione){}
}
publiclongnumberTokenizer(Strings)
throwsIOException
{
StringTokenizerst=
newStringTokenizer(s,"");
returnInteger.valueOf((st.nextToken())).longValue();
}
publicvoidfindData(longfind)//查找数据
throwsIOException,NumberNotFoundException
{
isFind=true;
Strings="",findString="";
longi;
DataMapdm=newDataMap();
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
while((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
//in.close();
try{
if(dm.containsKey(String.valueOf(find).toString()))
{
findString=dm.get(find);
System.out.println("学号"+find+"先生的材料是:");
System.out.println(findString);
}else
thrownewNumberNotFoundException();
}catch(NumberNotFoundExceptione){
System.out.println(find+"nofound:(");
isFind=false;
}
}
publicstaticvoidprint()//读取文本文件把数据打印到终真个办法
throwsIOException
{
try{
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
Stringread="";
while((read=in.readLine())!=null)
System.out.println(read);
}catch(IOExceptione){}
}
publicstaticvoidtimeOut(doublesec)//停留长久工夫的一个办法完整能够不要这个功效
{
doubleseconds=sec;
longt=System.currentTimeMillis()+(int)(seconds*1000);
while((System.currentTimeMillis())<t)
;
}
publicvoidnumSort()//按学号排序
throwsIOException
{
longi=0;
Strings="";
try{
DataArrayListdal=newDataArrayList();
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
while((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
Collections.sort(dal);
DataMapdm=newDataMap();
BufferedReaderin2=
newBufferedReader(
newFileReader("data.txt"));
while((s=in2.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
PrintWriterout=
newPrintWriter(
newBufferedWriter(
newFileWriter("data.txt")));
Iteratorit=dal.iterator();
longtemp=0;
StringtempStr="";
while(it.hasNext())
{
temp=Integer.valueOf((String)it.next()).longValue();
tempStr=dm.get(temp);
out.println(tempStr);
}
out.close();
}catch(IOExceptione){}
}
publicvoidrewrite()
throwsIOException,NumberNotFoundException
{
try{
System.out.print("请输出你要修正的先生学号:");
BufferedReaderin=
newBufferedReader(
newInputStreamReader(System.in));
StringinputLine=in.readLine();
longnum=Integer.valueOf(inputLine).longValue();
findData(num);
if(isFind)
{
deleteData(num);
System.out.print("请从头输出该先生的材料:");
Stringstr=inputData();
addData(str);
System.out.println("rewritetrue!");
}
}catch(IOExceptione){}
catch(NumberNotFoundExceptione){}
}
publicintcount()
throwsIOException
{
DataArrayListdal=newDataArrayList();
try{
Strings="";
longi=0;
BufferedReaderin=
newBufferedReader(
newFileReader("data.txt"));
while((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
}catch(IOExceptione){}
returndal.size();
}
}
/*
*
*@authorRangWei
*TODO这是个我们写的一个容器,承继大众类HashMap
*也许的功效就相称一个数组
*
*/
classDataMapextendsHashMap//一个存储数据的Map
{
publicvoidput(longi,Stringstr)//把学号和数据放进这个Map
{//今后一个学号(key)对应的是一团体的数据(value)
put(String.valueOf(i).toString(),str);
}
publicvoidremove(longi)//吸收学号,然后删除学号(key)和它对应的数据(value)
{
remove(String.valueOf(i).toString().toString());
}
publicStringget(longi)//吸收一个学号,然后前往这个key对应的value
{
Strings=String.valueOf(i).toString();
if(!containsKey(s))
{
System.err.println("NotfoundKey:"+s);
}
return(String)get(s);
}
}
/*
*
*@authorRangWei
*
*TODO这个类承继ArrayList
*用来按数字排序,在用学号排序时要用到它
*
*/
classDataArrayListextendsArrayList
{
publicvoidadd(longnum)
{
StringnumToString=String.valueOf(num).toString();
add(numToString);
}
}
/*
*
*@authorRangWei
*
*TODO增添的一个Exception,次要是在文件里没有要找
*的学号就抛出
*
*/
classNumberNotFoundExceptionextendsException
{
publicNumberNotFoundException()
{}
}
首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|