java通讯录文本输入_输入输出流实例1 文件通讯录

packageunit6;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Scanner;public classAddreBookTest {public static intdisplayMenu() {

System.out.println("\t\t************************************");

System.out.println("\t\t* 1------create a new one *");

System.out.println("\t\t* 2------show all *");

System.out.println("\t\t* 3------exit *");

System.out.println("\t\t************************************");try{int choice =System.in.read();

System.in.skip(2);if (choice >= 49 && choice <= 52) {return choice - 48;

}else{return 0;

}

}catch(IOException e) {

System.out.println("ERROR!");return 0;

}

}public static void main(String[] args)throwsException {inti;while(true){

i=displayMenu();switch(i){case 1:

newAddressBook();break;case 2:

displayAddressBook();break;case 3:

System.exit(0);break;case 4:default:

{System.out.println("chose error!");}

}

}

}public static booleanyesno(String msg){

Scanner keyin= newScanner(System.in);

System.out.println(msg);char ch = keyin.nextLine().charAt(0);if(ch=='y'||ch=='Y') return true;else return false;

}public static voidnewAddressBook(){

System.out.println("========>creat a new AddressBook<=========");

System.out.println("attention: the maxnum of addressbook is no more than 200");

Person[] addressbook= new Person[200];int i=0;boolean yesno =true;while(i<200){

Person tmpPerson= newPerson();

tmpPerson.inputDate();

yesno=yesno("are you sure?(y/n");if(yesno){

addressbook[i]=tmpPerson;

}else{

System.out.println("please retry");continue;

}

yesno=yesno("continue?(y/n)");if(!yesno){break;

}

i++;

}

BufferedWriter addressFile=null;try{

addressFile= new BufferedWriter(new FileWriter ("addressbook.txt"));for(int k=0;k

addressbook[k].writeToStream(addressFile);

}

}catch(IOException e1){

e1.printStackTrace();

}finally{try{if(addressFile!=null) addressFile.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}

System.out.println("done!");

}public static voiddisplayAddressBook(){

Person[]addressbook=new Person[200];

BufferedReader filein= null;try{

filein= new BufferedReader(new FileReader("addressbook.txt"));int i=0;

Person tmpPerson= newPerson();boolean flag =tmpPerson.readFromStream(filein);while(flag){

addressbook[i]=tmpPerson;

addressbook[i].display();

i++;

tmpPerson=newPerson();

flag=tmpPerson.readFromStream(filein);

}

filein.close();

}catch(IOException e1){

e1.printStackTrace();

}catch(Exception e2){

e2.printStackTrace();

}finally{try{if(filein!=null){

filein.close();

}

}catch(Exception e){

}

}

System.out.println("通讯录显示结束");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值