正如所承诺的,这里是 HashSet 序列化/反序列化代码:
import java.util.*;
import java.io.*;
class UserPasswordMap{
private HashMap<String,String> userMap;
private String m_filename;
public UserPasswordMap()
{
userMap = null;
m_filename = null;
userMap = new HashMap<String,String>();
}
public UserPasswordMap(String filename, String credo)
{
try{
m_filename = filename;
File f = new File(m_filename);
userMap = new HashMap<String,String>();
if(f.exists() && (!(f.canRead() && f.canWrite()))){
System.err.println("Oops, Insufficient permissions to read/write for filename: "+m_filename);
}else{
f.createNewFile();
}
}catch(Exception e){
System.err.println(e.getMessage());
}
if(credo.equals("D")){
try{
ReadMap(filename);
}catch(Exception e){ System.err.println(e.getMessage());}
}
}
public void ReadMap(String filename) throws IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
userMap = (HashMap<String,String>)ois.readObject();
fis.close();
}
public void WriteMap(String filename) throws IOException{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userMap);
oos.close();
}
public ArrayList<String> getUsers()
{
ArrayList<String> users = new ArrayList<String>();
if(userMap == null){
return null;/*or return users*/
}else{
for(String s:userMap.keySet()){
users.add(s);
}
}
return users;
}
public ArrayList<String> getPasswords()
{
ArrayList<String> passwords = new ArrayList<String>();
if(userMap == null){
return null;/*or return users*/
}else{
for(String s:userMap.values()){
passwords.add(s);
}
}
return passwords;
}
public String getPassword(String username)
{
return userMap.get(username);
}
public void addUser(String username, String password){
userMap.put(username, password);
try{
WriteMap(m_filename);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
public void saveData(){
if(m_filename == null){
System.err.println("File-Name is not supplied");
}else{
try{
WriteMap(m_filename);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}
}
和测试代码:
public class Main{
private UserPasswordMap map;
public static void main(String[] args){
Main obj = new Main();
obj.init();
Scanner scan = new Scanner(System.in);
System.out.println("0. Init With Serialisation(CAN THROW ERROR)");
while(true){
System.out.println("1. Add New User");
System.out.println("2. Query Permissions");
System.out.println("3. Exit");
int data = scan.nextInt();
switch(data){
case 0: obj.initWithSerialisation();
break;
case 1: obj.addNewUser();
break;
case 2: obj.queryPerms();
break;
case 3: System.exit(0);
}
}
}
public void init()
{
map = new UserPasswordMap("try1.ser","");
}
public void initWithSerialisation()
{
map = new UserPasswordMap("try1.ser","D");
}
private Scanner scan;
public void addNewUser()
{
System.out.print("Enter User:");
scan = new Scanner(System.in);
String username = scan.nextLine();
System.out.print("Enter Pass:");
String password = scan.nextLine();
map.addUser(username, password);
}
public void queryPerms()
{
System.out.print("Enter User:");
if(scan == null){ scan = new Scanner(System.in);}
String username = scan.nextLine();
System.out.print("Enter Pass:");
String pass = scan.nextLine();
if(map.getUsers().contains(username) && map.getPassword(username).equals(pass)){
System.out.println("authenticated!");
}else{
System.out.println("Oops Wrong credentials!");
}
}
}