import java.io.*;
import java.util.*;
public class LittleProgram
{
static boolean isDelete = true;
static boolean isFind = true;
public static void main(String [] args)//主方法,程序从这里开始运行
throws IOException,NumberNotFoundException
{
int choice=-1;
do{
LittleProgram lp = new LittleProgram();
System.out.println();
System.out.println("\t#############################");
System.out.println();
System.out.println("\t\t 通訊錄管理系统");
System.out.println("\t
System.out.println();
System.out.println("\t#############################\n");
System.out.print("1.增加号码:\n"+
请用号码插入,查找,修改,删除数据");
"2.查找号码:\n"+
"3.删除号码:\n"+
"4.清除所有号码:\n"+
"5.把号码全部打印到屏幕\n"+
"6.把通讯录按号码排序\n"+
"7.修改号码\n"+
"8.统计通码讯录的总人数\n"+
"9.关于作者\n"+
"0.退出程序.\n" +
"输入:");
BufferedReader in =
new BufferedReader(
//从终
//端接
new InputStreamReader(System.in));//收数
String inputLine = in.readLine();
choice= Integer.valueOf(inputLine).intValue();//项;
switch(choice)
{
case 1: {//1.增加号码
//字选
String str = lp.inputData();
lp.addData(str);
System.out.println("增加号码成功.");
timeOut(1);
}break;
case 2: {//2.查找号码
long find = 0;
System.out.print("请输入你要查找的号码:");
BufferedReader inn =
new BufferedReader(
new InputStreamReader(System.in));
String inputLi = inn.readLine();
find = Integer.valueOf(inputLi).longValue();
lp.findData(find);
timeOut(2);
}break;
case 3: {//3.删除号码
long deleteNumber = 0;
System.out.print("请输入你想删除号码:");
BufferedReader bf =
new BufferedReader (
new InputStreamReader(System.in));
String inputL = bf.readLine();
deleteNumber = Integer.valueOf(inputL).longValue();
lp.deleteData(deleteNumber);
if(isDelete)
System.out.println("删除号码成功!");
timeOut(1);
}break;
case 4: {
lp.clearData();//4.清除所有号码
timeOut(1);
}break;
case 5: {
print();//5.把号码全部打印到屏幕
timeOut(2);
}break;
case 6: {
lp.numSort();//6.把号码按号码排序
System.out.println("按照号码从小到大排序成功!\n"+
"排序后:\n");
print();
timeOut(2);
}break;
case 7: {
lp.rewrite();//7.修改号码
timeOut(2);
}break;
case 8: {
int count = lp.count();
System.out.println("共有"+count+"个号码记录.");
timeOut(2);
}break;
case 9: {
System.out.print("\t\t 李雪萍\n"+
"\t\t 安徽理工大學理學院\n"+
"\t\tQQ:1154646392\n");
timeOut(4);
}break;
}}while (choice != 0);
System.out.println("Bye! ^-^");
System.exit(0);
}
public String inputData()//从终端接收数据的方法,返回字符串
throws IOException,NumberFormatException
{
System.out.print("请依次输入 :号码 姓名 地址 生日\n" +
"每项数据请用空格隔开:");
String all = "";
try{
BufferedReader in =
new BufferedReader (
//从终
//端接
来提取每个字符串
new InputStreamReader(System.in));
//收数
String inputLine = in.readLine();
StringTokenizer str =
//据
new StringTokenizer(inputLine," ");//接收的数据用空格隔开,这个类用
long num = Integer.valueOf(str.nextToken()).longValue();//号码
String name = (String)str.nextToken();
String add = (String)str.nextToken();
String birth = (String)str.nextToken();//出生年月
all = String.valueOf(num) +" , "+
name +" , "+
add +" , "+
String.valueOf(birth);//把所有的数据用" , "隔开然后在连起来放进字符串
//姓名
// 地址
all
}catch (IOException e){}
catch (NumberFormatException e){}
return all;//返回字符串 all
}
public void addData(String str)//增加号码的方法
throws IOException
{
String s1 ="",s2="" ,s3= "";
File file = new File("c:\\data.txt");
if (file.exists())//如果文件 data.txt 存在
{
try{
BufferedReader in =
new BufferedReader(
new FileReader(file));
while ((s1=in.readLine())!=null)
s2+=s1+"\n";//把文件中的每行数据全部放进一个字符串 s2
s2+=str+"\n";
BufferedReader in2 =
//再把 s2 于形参 str 相连放进 s2
new BufferedReader(
new StringReader(s2));
PrintWriter out =
new PrintWriter(
的一行数据)
new BufferedWriter(
//把字符
//串 s2 也
//就是原
//文件+
//形参 str(新输入
//重新写进 data.txt
new FileWriter(file)));
//覆盖原来的数据
while ((s3=in2.readLine())!= null)
{
out.println(s3);
}
out.close();
//System.out.println("write data true.");
}catch (IOException e){}
}else{
System.err.println("File \"data\" Missing!");
}
}
public void clearData()//清除 data.txt 的所有数据的方法
throws IOException
{
File file = new File("c:\\data.txt");
if(file.exists())//如果文件在
{
try{
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter(file)));
out.print("");//在文件 data.txt 里写进一个空字符,所以清除了原来的内容
out.close();
System.out.println("clear data true!");
//关闭文件
}catch(IOException e){}
}else{
}
System.err.println("File \"data\" Missing!");
}
public void deleteData(long deleteNumber)//删除某条号码数据
throws IOException,FileNotFoundException
{
isDelete = true;
try{
DataMap mp = new DataMap();//生成一个自己编写的容器
long j=0;
String s1="",s2="",s3="";
BufferedReader in =
new BufferedReader(
new FileReader("c:\\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
throw new NumberNotFoundException();
Collection c = mp.values();
Iterator iter = c.iterator();
while(iter.hasNext())
{
s1 = (String)iter.next();
s3 +=s1+"\n";
}
BufferedReader in2 =
new BufferedReader(
new StringReader(s3));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("c:\\data.txt")));
//System.out.println("delete No"+deleteNumber);
while( (s1 = in2.readLine())!=null)
{
out.println(s1);
}
out.close();
}catch (NumberNotFoundException e)
{
isDelete = false;
System.out.println(deleteNumber+" no found :(");
}
}catch(IOException e){}
}
public long numberTokenizer(String s)
throws IOException
{
StringTokenizer st =
new StringTokenizer(s," ");
return Integer.valueOf((st.nextToken())).longValue();
}
public void findData(long find)//查找数据
throws IOException,NumberNotFoundException
{
isFind = true;
String s = "",findString ="";
long i;
DataMap dm = new DataMap();
BufferedReader in =
new BufferedReader(
new FileReader("c:\\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
throw new NumberNotFoundException();
}catch (NumberNotFoundException e){
System.out.println(find+" no found :(");
isFind = false;
}
}
public static void print()//读取文本文件把数据打印到终端的方法
throws IOException
{
try{
BufferedReader in =
new BufferedReader(
new FileReader("c:\\data.txt"));
String read = "";
while ((read = in.readLine())!=null)
System.out.println(read);
}catch(IOException e){}
}
public static void timeOut(double sec)//停顿短暂时间的一个方法完全可以不要这个功能
{
double seconds = sec;
long t = System.currentTimeMillis()+(int)(seconds*1000);
while ((System.currentTimeMillis())
dm.put(i,s);
}
PrintWriter out =
new PrintWriter (
new BufferedWriter(
new FileWriter("c:\\data.txt")));
Iterator it = dal.iterator();
long temp = 0;
String tempStr = "";
while (it.hasNext())
{
temp = Integer.valueOf((String)it.next()).longValue();
tempStr = dm.get(temp);
out.println(tempStr);
}
out.close();
}catch(IOException e){}
}
public void rewrite()
throws IOException,NumberNotFoundException
{
try{
System.out.print("请输入你要修改的学生号码:");
BufferedReader in =
new BufferedReader (
new InputStreamReader(System.in));
String inputLine = in.readLine();
long num = Integer.valueOf(inputLine).longValue();
findData(num);
if(isFind)
{
deleteData(num);
System.out.print("请重新输入该号码:");
String str = inputData();
addData(str);
System.out.println("rewrite true!");
}
}catch(IOException e){}
catch(NumberNotFoundException e){}
}
public int count()
throws IOException
{