首页>代码>java se基于IO流的手机联系人管理系统>/手机通讯录/src/MobileContacts/dao/Impl/MContactsDaoImpl.java
package MobileContacts.dao.Impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;

import MobileContacts.entity.Contacts;

public class MContactsDaoImpl implements MContactsdao {
	private List<Contacts> list = null;
	private final String FILE_PATH = "contacts.dat";
	File file= new File(FILE_PATH);
	
	public MContactsDaoImpl(){
		this.list =new ArrayList<>();
		if(file.length()!=0&&file.length()<4){
			this.list.addAll(readObj());
		}
	}
	public List<Contacts> getMenus() {
		return this.list;
	}
	private List<Contacts> readObj() {
		List<Contacts> ct =new ArrayList<>();
		ObjectInputStream ois =null;
		Contacts con =null;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));
			while((con = (Contacts) ois.readObject())!=null){
				ct.add(con);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		Collections.sort(ct,new Comparator<Contacts>() {
			
			public int compare(Contacts con1,Contacts con2){
				return con2.getName().compareTo(con1.getName());
			}
		});
		return ct;
	}
	@Override
	public List<Contacts> selectAllContactss() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertContacts(Contacts Contacts) {
		// TODO Auto-generated method stub
		this.list.add(Contacts);
		writerObj();
	}

	private void writerObj() {
		// TODO Auto-generated method stub
		ObjectOutputStream oos=null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			for(Contacts c:this.list){
				oos.writeObject(c);
			}
			//oos.write(null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(oos!=null){
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}

	@Override
	public void deleteContacts(Contacts Contacts) {
		// 删除
		Contacts con =null;
		for(Contacts c:list){
			if(Contacts.getName().equals(c.getName())){
				con =c;
				break;
			}
		}
		this.list.remove(con);
		writerObj();
	}

	@Override
	public void alterContacts(String name, Double price) {
		// TODO Auto-generated method stub

	}

	@Override
	public Contacts getContactsByName(String name) {
		// 按照名字查找
		if(this.list.isEmpty()){
			return null;
		}else{
			for(Contacts c:list){
				if(name.equals(c.getName())){
					return c;
				}
			}
		}
		return null;
	}

	@Override
	public Contacts getContactsByweChar(String weChar) {
		// 按微信号查找
		if(this.list.isEmpty()){
			return null;
		}else{
			for(Contacts c:list){
				if(weChar.equals(c.getWeChar())){
					return c;
				}
			}
		}
		return null;
	}

}
最近下载更多
asdgcyv  LV1 2023年5月31日
15342201772  LV7 2022年12月4日
balabalawuyu  LV6 2022年11月24日
1102445814  LV1 2022年6月25日
杨小姐好棒棒  LV3 2022年5月24日
Demo1111  LV30 2021年12月6日
N  LV1 2021年6月17日
MrLinsir  LV2 2020年6月19日
如夝萌灵  LV1 2020年5月13日
byy1314520  LV1 2020年4月18日
最近浏览更多
暂无贡献等级
asdgcyv  LV1 2023年5月31日
shushs  LV1 2023年5月31日
adminadminsqwqe  LV7 2023年1月30日
Willson88888  LV3 2022年12月13日
15342201772  LV7 2022年12月4日
balabalawuyu  LV6 2022年11月24日
飞梦ff  LV8 2022年7月24日
1102445814  LV1 2022年6月25日
又见梨园  LV1 2022年6月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友