随笔 - 40, 文章 - 0, 评论 - 19, 引用 - 0
数据加载中……

Java实验之简易通讯录

Java对文件进行了读写操作,信息存储在phonesnumber.txt里面

//Main.java
package exp5_5;

import java.io.IOException;

public class Main {

    
/**
     * 
@throws IOException 
     * @Hadn't 2010-4-23
     
*/


    
public static void main(String[] args) throws IOException {
        addresslist read 
= new addresslist("phonesnumber");
        read.show();
    }


}

//people.java
package exp5_5;

public class people {
    
private String PhoneNum;
    
private String Name;
    
private int No;
    
public people(String Name ,String PhoneNum,int No){
        
this.Name = Name;
        
this.PhoneNum = PhoneNum;
        
this.No = No;
    }

    
public String get_Name(){
        
return this.Name;
    }

    
public String get_PhoneNum(){
        
return this.PhoneNum;
    }

    
public int get_No(){
        
return this.No;
    }

}

//addresslist.java
package exp5_5;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


/**
 * @Hadn't 2010-4-22
 
*/

public class addresslist {
    
private int Num;
    File fileadd;    
    
public addresslist(String fileadd) throws IOException{
        
this.fileadd = new File(fileadd+".txt");    
            
if((!this.fileadd.exists())){
                
this.fileadd.createNewFile();
            }

            
else{
                Scanner read 
= new Scanner(this.fileadd);
                
this.Num  = 0;
                
while(read.hasNext()){
                    
for(int i = 0 ; i < 3 ; i++ )
                        read.next();
                    
this.Num++;
                }

            }

        }
    
    
public boolean insert(people per) throws IOException{    
        
// 保存    
        FileWriter writer = new FileWriter(this.fileadd,true);    
        writer.write(per.get_No()
+" "+per.get_Name()+" "+per.get_PhoneNum()+"\n");
        writer.close();
        
return true;
    }

    
public void show() throws FileNotFoundException{
        
while(true){
            
// wait
            Scanner read = new Scanner(this.fileadd);
            
            
int i = 0;
            
while(read.hasNext()){
                System.out.println(
"No."+read.next()+"  姓名:"+read.next()+"   电话号码:"+read.next());
                i
++;
            }

            System.out.println(
"目前记录总数:"+i);
            
// show
            System.out.println("退出输入exit,插入数据输入1");
            Scanner cin 
= new Scanner(System.in);            
            String get 
= cin.nextLine();            
            
if(get.equals("exit")){            
                
break;                
            }

            
else if(get.equals("1")){
                System.out.println(
"请输入姓名,不要输入非法字符或空格");
                String Name 
= cin.next();
                System.out.println(
"请输入电话号码,不要输入非法字符或空格");
                String PhoneNumber 
= cin.next();
                people a 
= new people(Name,PhoneNumber,this.Num++);                    
                
try {
                    insert(a);
                }
 catch (IOException e) {                    
                    e.printStackTrace();
                }

            }
            
        }
        
    }

}

posted on 2010-04-26 21:20 hadn't 阅读(572) 评论(2)  编辑 收藏 引用

评论

# re: Java实验之简易通讯录  回复  更多评论   

当年的大作业啊……
2010-04-27 08:17 | 陈梓瀚(vczh)

# re: Java实验之简易通讯录  回复  更多评论   

@陈梓瀚(vczh)
呵呵 Java入门~ 见笑啦~ 拜阅了贵博 希望有机会能与您多交流向您学习
2010-04-27 10:44 | hadn't

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理