java 时间简单电话簿 初学者入门(2) 读写文件

package test;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class MirrorTelphoneBook {

    public static Scanner in = new Scanner(System.in);
    
    // 用来保存 电话簿信息
    public static Map<String, String> phoneInfoMap = new LinkedHashMap<String, String>();

    private final static String fileName = "D:\\Tel.txt";
    
    // 系统分割符
    String l = System.getProperty("line.separator");
    
    public static void main(String[] args) {
        
        MirrorTelphoneBook instance = new MirrorTelphoneBook();
        // 首先初始化内存将文件数据读入phoneInfoMap
        instance.read(fileName);
        do {
            System.out.println("欢迎使用本电话簿");
            int job = 0;
            System.out.println("功能选择:1.存储 2.查询  0.退出");
            job = in.nextInt();
            switch (job) {
                case 0: break;
                case 1: instance.saveNum(); break;
                case 2: instance.getNum(); break;
            }
            System.out.println("是否要继续?(yes/no)");
        } while (in.next().equals("yes"));
        instance.writeIn();
        System.out.println("拜拜~");
        System.exit(0);
    }

    /**
     * Description: 保存电话<br>
     * 
     * @author wangmingyang<br>
     * @taskId <br>
     * <br>
     */
    public void saveNum() {
        do {
            System.out.println("姓名?");
            String k = in.next();
            System.out.println("电话?");
            String v = in.next();
            phoneInfoMap.put(k, v);
            System.out.println("是否要继续保存?(yes/no)");
        } while (in.next().equals("yes"));
    }

    public void getNum() {

        do {
            System.out.println("请输入需要查询的姓名");
            String k = in.next();
            if (phoneInfoMap.get(k) != null) {
                System.out.println(k + "的电话是" + phoneInfoMap.get(k));
            } else {
                System.out.println("没搜到该联系人");
            }
            System.out.println("是否要继续查询(yes/no)");
        } while (in.next().equals("yes"));

    }

    /**
     * Description: 将文件内容读入phoneInfoMap中<br>
     * 
     * @author wangmingyang<br>
     * @taskId <br>
     * <br>
     */
    public void read(String fileName) {

        try {
            
            File file =new File(fileName);
            if(!file.exists())
            {       
                file.createNewFile();
            }
            
            FileReader e = new FileReader(fileName);
            BufferedReader bf = new BufferedReader(e);
            String t = "";
            while ((t = bf.readLine()) != null) {
                phoneInfoMap.put(t.split(":")[0], t.split(":")[1]);
            }
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Description: 将phoneInfoMap写进文件<br>
     * 
     * @author wangmingyang<br>
     * @taskId <br>
     * <br>
     */
    public void writeIn() {

        try {
            StringBuffer e = new StringBuffer();
            FileWriter tel = new FileWriter("D:\\Tel.txt");
            Set<?> set = phoneInfoMap.entrySet();
            Iterator<?> i = set.iterator();
            while (i.hasNext()) {
                Entry<?, ?> entry = (Entry<?, ?>) i.next();
                e.append(entry.getKey() + ":" + entry.getValue() + this.l);
            }
            tel.write(e.toString());
            tel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



你可能感兴趣的:(java)