使用IO流实现电话本管理系统

本文介绍了一个使用Java IO流实现的电话本管理系统,该系统能够完成添加、删除、修改和查询联系人的基本操作。通过读写文件的方式,实现了联系人信息的持久化存储。

使用IO流实现电话本管理系统

案例要求(IO流)

将电话本信息,写入文件,并能实现增删改查功能。

示例代码

package com.ambow.day0509;

import java.io.*;
import java.util.Scanner;

public class PhoneSys {
    static Scanner scanner = new Scanner(System.in);
    static File phones = new File("D:" + File.separator + "phones.txt");

    //主方法
    public static void main(String[] args){
        //文件不存在,则创建
        if (!phones.exists()){
            try {
                phones.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        while(true) {
            System.out.println("*******电话本管理系统******");
            System.out.println("*******1.添加联系人******");
            System.out.println("*******2.修改联系人******");
            System.out.println("*******3.删除联系人******");
            System.out.println("*******4.查询联系人******");
            System.out.println("*******0.退出通讯录******");
            System.out.println("请选择:");
            int choose = scanner.nextInt();

            switch (choose) {
                case 1:
                    addContacts();//添加联系人
                    break;
                case 2:
                    updateContacts();//修改联系人
                    break;
                case 3:
                    deleteContacts();//删除联系人
                    break;
                case 4:
                    queryContacts();//查询联系人
                    break;
                case 0:
                    System.out.println("退出系统");
                    System.exit(0);
                    break;
                default:
                    System.out.println("您的选择有误!");
                    break;
            }
        }
    }

    //查询所有联系人
    private static void queryContacts() {
        System.out.println(readFile());
    }

    //删除联系人
    private static void deleteContacts() {
        System.out.println("请输入要删除的联系人姓名:");
        String name = scanner.next();

        //获取所有联系人信息
        String allContacts = readFile();

        int nameIndex = allContacts.indexOf(name);
        if (nameIndex == -1){
            System.out.println("查无此人");
            return;
        }

        int endIndex = allContacts.indexOf("\n",nameIndex);
        String info = allContacts.substring(nameIndex,endIndex + 1);

        allContacts = allContacts.replaceAll(info,"");
        updateFile(allContacts);

        System.out.println("删除成功!");
    }

    //大家自己实现
    private static void updateContacts() {

    }

    //添加联系人
    private static void addContacts() {
        StringBuilder info = new StringBuilder();

        System.out.println("姓名:");
        String name = scanner.next();
        System.out.println("性别:");
        String sex = scanner.next();
        System.out.println("手机号码:");
        String phone = scanner.next();

        info.append(name).append(";").append(sex).append(";").append(phone);

        addToFile(info.toString());
    }

    //读文件
    public static String  readFile(){
        StringBuilder stringBuilder = new StringBuilder();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(phones);
            br = new BufferedReader(fr);

            String s = null;
            while ((s = br.readLine()) != null){
                //特别注意:只加\n不能实现换行,要换行,需要加\r\n,表示回车换行。
                stringBuilder.append(s).append("\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return  stringBuilder.toString();
    }

    //添加联系人到文件
    public static void addToFile(String info){
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(phones,true);
            bw = new BufferedWriter(fw);

            bw.write(info);
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //更新文件
    public static void updateFile(String all){
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(phones);
            bw = new BufferedWriter(fw);

            bw.write(all);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意:以上修改联系人功能未实现,可参考删除联系人自行实现。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值