能保存操作记录的java电话簿

之前想写一个电话簿,看了网上的很多都不能保存操作记录,现在自己写了一个能够将数据操作记录保存在txt文件中的电话簿小程序,实现了增删改查以及查询全部的功能。代码如下:
package dianhuaben;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	int i=0;
    Scanner sc = new Scanner(System.in);
File file=new File("word.txt");
    public Main() throws IOException {
        xuanZe();
    }
    public void xuanZe() throws IOException {
        System.out.println("----------电话本管理系统----------");
        System.out.println("1.添加 2.删除 3.修改 4.查询所有 5.根据姓名查询 6.退出");
        System.out.println("----------电话本管理系统----------\n 请选择操作:");
        String a = sc.next();
        switch (a) {
        case "1":
            add();
            break;
        case "2":
            delete();
            break;
        case "3":
            xiuGai();
            break;
        case "4":
            selectAll();
            break;
        case "5":
            selectByName();
            break;
        case "6":
            tuiChu();
            break;
        default:
            System.out.println("输入指令错误!请重新输入!");
            xuanZe();
            break;
        }
    }
    public void add() throws IOException {
        ZiLiao b = new ZiLiao();
        System.out.println("------添加电话本------");
        System.out.println("请输入相关信息:");
        System.out.print("姓名:");
        String name = sc.next();
        b.setName(name);          
        System.out.print("电话:");
        String dianhua = sc.next();
        b.setPhoneNumber(dianhua);
        FileWriter writer;	
		try {
			writer = new FileWriter("word.txt",true);
			writer.write(name);
			writer.write(":");
			writer.write(dianhua);
			writer.write("\r\n");			
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        System.out.println("添加成功");
        System.out.println(b.toString());
        xuanZe();
    }
    public void delete() throws IOException {    	
        System.out.println("------删除电话本------");
        System.out.println("请输入你要删除的电话本:");
        String s=sc.next();
try {       	
        	BufferedReader br=new BufferedReader(new FileReader(file));        	
        			ArrayList<String>list=new ArrayList<>();
        			String str;
        			while(true) {
        				str=br.readLine();
        				if(str==null) break;
        				String[] temp=str.split(":");
        				String na=temp[0];
        				String dh=temp[1];
        				int index=str.indexOf(s);      				
        				if(index!=-1) {
        					i=5;           					
        					System.out.println("删除成功!");

        					continue;    	                  
        				}        			        				
        				list.add(str);       				
        			}        			
        			if(i!=5) {
    					System.out.println("你输入的账号不存在!请重新输入!");
                        xiuGai();
    				}
        			file.delete();
        			file.createNewFile();
        			FileWriter fw=new FileWriter(file);
        	for(int i=0;i<list.size();i++) {
        		String str1=(String)list.get(i);
        		fw.write(str1+"\r\n");
        	}
        	br.close();
        	fw.close();
        	xuanZe();
        }
        catch(Exception e) {
        	e.printStackTrace();
        }       
    }
    public void xiuGai() throws IOException {
        System.out.println("--------------修改电话本-------------");
        System.out.println("请输入你要修改的账号:");
        String s = sc.next();
        try {        	
        	BufferedReader br=new BufferedReader(new FileReader(file));        	       			        			
        			ArrayList<String>list=new ArrayList<>();
        			String str;
        			while(true) {
        				str=br.readLine();
        				if(str==null) break;
        				String[] temp=str.split(":");
        				String na=temp[0];
        				String dh=temp[1];
        				int index=str.indexOf(s);        				
        				if(index!=-1) {
        					System.out.println("你要修改的是:" + str);
        					System.out.println("请重新输入新号码:");
        					String s1=sc.next();        					
        					str=na+":"+s1;       					
        					 System.out.print("修改成功!");
        	                    System.out.println("修改后为:" + s+":"+s1);
        	              i=5;       
        				}       				        				
        				list.add(str);        				
        			}        			
        			if(i!=5) {
    					System.out.println("你输入的账号不存在!请重新输入!");
                        xiuGai();
    				}
        			file.delete();
        			file.createNewFile();
        			FileWriter fw=new FileWriter(file);       	
        	for(int i=0;i<list.size();i++) {
        		String str1=(String)list.get(i);
        		fw.write(str1+"\r\n");
        	}
        	br.close();
        	fw.close();
        	xuanZe();
        }
        catch(Exception e) {
        	e.printStackTrace();
        }        
    }   
    public void selectAll() throws IOException {
    	BufferedReader br=new BufferedReader(new FileReader(file));
    	String line=null;
    	System.out.println("--------------所有电话信息---------------");
    	while((line=br.readLine())!=null) {
    		String[] temp=line.split(":");
    	String na=temp[0];
    	String dh=temp[1];       
        System.out.println("姓名:"+na+" "+" "+"电话:"+dh);}        
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        xuanZe();
    }
    public void selectByName() throws IOException {
    	BufferedReader br=new BufferedReader(new FileReader(file));
    	String line=null;
    	System.out.print("请输入你要查询的姓名:");
    	String s=sc.next();
    	while((line=br.readLine())!=null) {
    		String[] temp=line.split(":");
    	String na=temp[0];
    	String dh=temp[1];
    	if(s.equals(na)) {
    		System.out.println("查询结果为:"+dh);
    		i=2;    		
    	}    	    		
    	}
    	if(i!=2)System.out.println("查无此人");
    	 xuanZe();
    	br.close(); 
        }
    public void tuiChu() {
        System.out.println("谢谢使用! \n");
        System.gc();// 系统回收资源
        Runtime.getRuntime().runFinalization();// 没起作用;
    }
    public void finalize() {// 并未被调用
        System.out.println("已回收资源,成功退出系统");
    }
    class ZiLiao { 
        private String name;        
        private String phoneNumber;      
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }       
        public String getPhoneNumber() {
            return phoneNumber;
        }
        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
        public String toString() {
            String s = "姓名:" + name +"电话:" +phoneNumber;
            return s;
        }
    }
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Main pb = new Main();    
    }
}

主要用到了io和集合;
运行效果

之前保存的信息,并没有丢失。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现了以下要求说明的功能: 1.要求使用GUI,采用 Swing 技术数据存储采用文件系统(可以是文本 文件、xml 文件、vCard 文件或其他自定义格式文件,但不能是 Excel 文件)。不 使用数据库。 2. 通讯录中每个项目包括: ( 1)基本信息:姓名、手机号码、电子邮箱、生日、所属组、备注、像片。 ( 2)工作单位:国家、省份、城市、地址、工作单位、所属部门、职位、 邮编、电话、传真 ( 3)网络信息:个人主页、即时通信工具及号码、备用邮箱 ( 4)家庭资料:国家、省份、城市、地址、邮编、电话 3 .通讯录数据的录入功能:输入并存储新的通讯录数据,输入时如果有同 名或同电话号码时,应该给出提示。 4 .通讯录数据的查询功能: ( 1)默认列出所有条目。 ( 2)模糊查询,可以按:姓名、电话号码、手机、姓名的汉语拼音的声母 或全拼查询。支持模糊查询即输入部分数据后,可以查询出符合条件的全部条目。 如:输入“张”,列出所有名字中有“张”的条目;输入“ 2645”列出电话中有 该 4个数字的所有条目;输入“xl ”,列出声母为该 2 个字母的所有条目 ( 3)指定分组后,列出某分组的所有条目 说明:查询结果如果有多个条目,应按照姓名排序,并使用姓或姓的第一 个汉语拼音的字母分类。 5 .删除和修改功能:查询到一个条目后,可以删除该条目或修改条目内容。 6 .通信录内容的导入和导出功能。要求能够以CSV 格式和 vCard 格式导入 和导出通讯录的全部内容。并能够与主流手机或其他通讯录软件交换数据。 7 .通讯录显示设置,可以设置在显示查询结果时,一个条目可以显示或不 显示哪些内容。 注意:本系统实现了读取和保存csv文件,同时csv文件中只保存了图片的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值