⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userfiledao.java

📁 java qq 实现点对点聊天功能 并提供服务器端聊天室元代码
💻 JAVA
字号:
package dao.impl;
import java.io.*;
import java.util.*;

import dao.UserDao;
import model.User;
public class UserFileDao implements UserDao {
	private String name;
	
	public UserFileDao(String name) {
		super();
		this.name = name;
	}

	public void saveUsers(Map m){
		ObjectOutputStream out=null;
		try {
			FileOutputStream fos=new FileOutputStream(name);
			out = new ObjectOutputStream(fos);
			
			Collection c=m.values();
			Iterator it=c.iterator();
			while(it.hasNext()){
				User ac=(User)it.next();
				out.writeObject(ac);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				if (out!=null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public Map loadUsers(){
		Map m=new HashMap();
		ObjectInputStream in=null;
		try {
			FileInputStream fis=new FileInputStream(name);
			in = new ObjectInputStream(fis);
			while(true){
				User ac=(User)in.readObject();
				m.put(new Integer(ac.getId()),ac);
			}
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		finally{
			try {
				if (in!=null) in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return m;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -