用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

PhoneBook

2018-05-26 作者: 云代码会员举报

[java]代码库

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.HashMap;  
import java.util.Map;  
  
public class IOU {  
    public static Map<String, PhoneBook> phoneDatas = new HashMap<String, PhoneBook>();  
    public static final String DATA_FILE = "data.bat";  
  
    static {  
        load();  
    }  
  
    public static boolean save(String name,String phoneNum){  
          
        phoneDatas.put(name, new PhoneBook(name,phoneNum));  
        try {  
            FileOutputStream out = new FileOutputStream(DATA_FILE);  
            out.write((name+" "+phoneNum).getBytes());  
            out.flush();  
            out.close();  
            return true;  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return false;  
    } 
    
    public static boolean update(String name, String phoneNum){  
        if(IOU.phoneDatas.containsKey(name)){  
            IOU.phoneDatas.get(name).setPhoneNum(phoneNum);  
        }          
        return false;  
    } 
        
    public static boolean load() {  
        try {  
            FileInputStream in = new FileInputStream(DATA_FILE);  
            byte[] bs = new byte[1024];  
            int len = 0;  
            StringBuilder dataStr = new StringBuilder();  
  
            while ((len = in.read(bs)) != -1) {  
                dataStr.append(new String(bs, 0, len));  
            }  
  
            String[] dataStrs = dataStr.toString().trim().split("\\s+");  
  
            if (dataStrs.length > 1) {  
                for (int i = 0; i < dataStrs.length; i += 2) {  
                    phoneDatas.put(dataStrs[i],new PhoneBook(dataStrs[i],dataStrs[i+1]));  
                }  
            }  
            return true;  
        } catch (FileNotFoundException e) {  
            try {  
                new File(DATA_FILE).createNewFile();  
            } catch (IOException e1) {  
                e1.printStackTrace();  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        return false;  
    }  


}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...