-2
我正在實現一個加密解密應用程序。爲此,我使用散列表來存儲具有提示的加密值。解密時,我需要獲取散列表values.but在這裏,我只得到一個單個密鑰及其在散列表中的值。其中storeMessage()用於存儲散列表到文件,getMessage()用於回憶。任何人都可以幫助我找到合適的解決方案。將值附加到散列表
在此先感謝
對於存儲哈希表提交
public boolean storeMessage(byte[] bt,String hint){
boolean status=false;
byte[][] valuePair=null;
java.util.Set<String> set=null;
java.util.Iterator<String> itr=null;
ObjectOutputStream objOut=null;
try {
System.out.println("jhkj:"+ht);
if(ht==null)
ht=new Hashtable<String, byte[]>();
ht.put(hint, bt);
keyPair=new String[ht.size()];
valuePair=new byte[keyPair.length][1];
set=ht.keySet();
itr=set.iterator();
int i=0;
while(itr.hasNext()){
keyPair[i]=itr.next();
valuePair[i]=ht.get(keyPair[i]);
i++;
}
if(!new java.io.File("store.db").exists())
new java.io.File(path+ "/store.db").createNewFile();
objOut=new ObjectOutputStream(new FileOutputStream(path+ "/store.db",true));
objOut.writeObject(keyPair);
objOut.writeObject(valuePair);
objOut.close();
status=true;
} catch (Exception e)
{
}
finally{
}
return status;
}
從文件
java.util.Hashtable<String, byte[]> getMessage(){
byte[][] valuePair=null;
ObjectInputStream objIn=null;
ht=new Hashtable<String, byte[]>();
try{
objIn=new ObjectInputStream(new FileInputStream(con.getFilesDir().getParentFile().getPath()+ "/store.db"));
keyPair=(String[]) objIn.readObject();
valuePair=(byte[][]) objIn.readObject();
int i=0;
while(i<keyPair.length){
ht.put(keyPair[i], valuePair[i]);
i++;
}
}
catch(FileNotFoundException ex)
{
ht=new Hashtable<String, byte[]>();
}
catch (Exception e)
{
System.out.println(">>>>>>"+e);
}
finally{
try {
objIn.close();
} catch (Exception e)
{
}
}
System.out.println("hash table:"+ht);
return ht;
}