• 输入输出流实例1 文件通讯录


    ##

    package unit6;
    import java.util.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    
    public class Person {
        private String name;
        private char sex;
        private String homePhone;
        private String officePhone;
        private String memo;
        public Person(){
            name="";
            sex='M';
        }
        public Person(String name){
            this.name=name;
        }
        public Person(String name,char sex,String homePhone){
            this.name=name;
            this.sex=sex;
            this.homePhone=homePhone;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public char getSex() {
            return sex;
        }
        public void setSex(char sex) {
            this.sex = sex;
        }
        public String getHomePhone() {
            return homePhone;
        }
        public void setHomePhone(String homePhone) {
            this.homePhone = homePhone;
        }
        public String getOfficePhone() {
            return officePhone;
        }
        public void setOfficePhone(String officePhone) {
            this.officePhone = officePhone;
        }
        public String getMemo() {
            return memo;
        }
        public void setMemo(String memo) {
            this.memo = memo;
        }
        public String toString(){
            String personinfo="=========================================
    ";
            personinfo+="姓名:"+name+" 性别:"+(sex=='M'?'男':'女')+"
    ";
            personinfo+="电话(H):"+homePhone+"
    ";
            personinfo+="电话(O):"+officePhone+"
    ";
            personinfo+="=============================================
    ";
            return personinfo;
        }
        public void display(){
            System.out.println(toString());
        }
        public void writeToStream(BufferedWriter out)throws IOException{
            out.write(name+","+(sex=='M'?'男':'女')+",");
            if(homePhone!=null) out.write(homePhone+",");
            else out.write(",");
            if(officePhone!=null) out.write(officePhone+",");
            else out.write(",");
            if(memo!=null) out.write(memo);
            else out.newLine();
        }
        public boolean readFromStream(BufferedReader in)throws IOException{
            String str = in.readLine();
            if(str=="end.") return false;
            String[] info=str.split(",");
            this.setName(info[0]);
            this.setSex(info[1].charAt(0));
            if(info[2]!=null) this.setHomePhone(info[2]);
            if(info[3]!=null) this.setOfficePhone(info[3]);
            if(info[4]!=null) this.setMemo(info[4]);
            return true;
        }
        public void inputDate(){
            Scanner in = new Scanner(System.in);
            System.out.print("name:");if(in.hasNext()) setName(in.next());
            System.out.print("sex:(F/M)"); if(in.hasNext()) setSex(in.next().charAt(0));
            System.out.print("homePhoe:");if(in.hasNext()) setHomePhone(in.next());
            System.out.print("officePhone:");if(in.hasNext()) setOfficePhone(in.next());
            System.out.print("memo:");if(in.hasNext()) setMemo(in.next());
        }
        
    
    }
    package unit6;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class AddreBookTest {
        public static int displayMenu() {
            System.out.println("		************************************");
            System.out.println("		*  1------create a new one         *");
            System.out.println("		*  2------show all                 *");
            System.out.println("		*  3------exit                     *");
            System.out.println("		************************************");
    
            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)throws Exception {
            int i;
            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 boolean yesno(String msg){
            Scanner keyin = new Scanner(System.in);
            System.out.println(msg);
            char ch = keyin.nextLine().charAt(0);
            if(ch=='y'||ch=='Y') return true;
            else return false;
        }
        public static void newAddressBook(){
            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= new Person();
                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<i;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 void displayAddressBook(){
            Person[]addressbook=new Person[200];
            BufferedReader filein= null;
            try{
                filein = new BufferedReader(new FileReader("addressbook.txt"));
                int i=0;
                Person tmpPerson = new Person();
                boolean flag = tmpPerson.readFromStream(filein);
                while(flag){
                    addressbook[i]=tmpPerson;
                    addressbook[i].display();
                    i++;
                    tmpPerson=new Person();
                    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("通讯录显示结束");
        }
        }
  • 相关阅读:
    【USACO2017JAN】 Promotion Counting
    【POJ 3468】 A Simple Problem with Integers
    【HDU 1754】 I Hate It
    【Codeforces 20C】 Dijkstra?
    【POJ 2407】 Relatives
    BZOJ5249 九省联考2018IIIDX(线段树+贪心)
    BZOJ5251 八省联考2018劈配(网络流)
    BZOJ4200 NOI2015小园丁与老司机(动态规划+上下界网络流)
    BZOJ3876 AHOI/JSOI2014支线剧情(上下界网络流)
    LOJ117 有源汇有上下界最小流(上下界网络流)
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5727647.html
Copyright © 2020-2023  润新知