在Java中将映射保存到文件时出现问题

在Java中将映射保存到文件时出现问题,java,file,encryption,save,maps,Java,File,Encryption,Save,Maps,我有两张生成得很好的地图。 现在我想停止生成它们,从文件开始工作 地图结构是 private Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>(); private Map<Integer, String> decodebook = new HashMap<Integer, String>(); private-Map-code

我有两张生成得很好的地图。 现在我想停止生成它们,从文件开始工作

地图结构是

private Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
private Map<Integer, String> decodebook = new HashMap<Integer, String>();
private-Map-codebook=new-HashMap();
私有映射decodebook=newhashmap();
下面是我用于保存和加载的代码。 解码本似乎工作得很好,它是代码本 我似乎无法加载回程序。恐怕有人看见了 由于我正在处理一个对象,很难判断它是否正确 甚至是正确的储蓄

我运行了一次程序,这段代码将与它一起运行

try {
        saveCodeBook(codebook, "CodeBook");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        saveDecodeBook(decodebook, "DecodeBook");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



public void saveCodeBook(Map<String, List<Integer>> obj,
        String filePath) throws IOException {
    OutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream(filePath));

        ((ObjectOutputStream) os).writeObject(obj);
    } catch (Exception ex) {
    } finally {
        os.close();
    }
}

public void saveDecodeBook(Map<Integer,String> obj,
        String filePath) throws IOException {
    OutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream(filePath));

        ((ObjectOutputStream) os).writeObject(obj);
    } catch (Exception ex) {
    } finally {
        os.close();
    }
}
试试看{
存储码本(codebook,简称“码本”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
存储解码书(decodebook,简称decodebook);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共无效存储码本(映射对象,
字符串文件路径)引发IOException{
OutputStream os=null;
试一试{
os=新对象输出流(新文件输出流(文件路径));
((ObjectOutputStream)os.writeObject(obj);
}捕获(例外情况除外){
}最后{
os.close();
}
}
公共存储簿(地图对象,
字符串文件路径)引发IOException{
OutputStream os=null;
试一试{
os=新对象输出流(新文件输出流(文件路径));
((ObjectOutputStream)os.writeObject(obj);
}捕获(例外情况除外){
}最后{
os.close();
}
}
第二次运行该程序时,我会注释掉上面的代码。 (不是直接在下面的这一块,它总是在左边)

private-Map-codebook=new-HashMap();
私有映射decodebook=newhashmap();
好的,一旦删除了上面的代码,我就再次运行这个程序,包括这段代码

public void loadBooks() throws IOException{

    loadCodeBook("CodeBook");
    //System.out.println(codebook);


    loadDeCodeBook("DecodeBook");
    //System.out.println(codebook);
    System.out.println(decodebook);

}

public Map<String, List<Integer>> loadCodeBook(String filePath)
        throws IOException {
    HashMap<String, List<Integer>> codebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        codebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.codebook = codebook;
}
public Map<Integer, String> loadDeCodeBook(String filePath)
        throws IOException {
    HashMap<Integer,String> decodebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        decodebook = (HashMap<Integer, String>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.decodebook = decodebook;
}
public void loadBooks()引发IOException{
载荷码本(“码本”);
//System.out.println(码本);
loadDeCodeBook(“DecodeBook”);
//System.out.println(码本);
System.out.println(解码本);
}
公共地图加载码本(字符串文件路径)
抛出IOException{
HashMap码本=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
codebook=(HashMap)((ObjectInputStream)是)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回this.codebook=codebook;
}
公共地图loadDeCodeBook(字符串文件路径)
抛出IOException{
HashMap decodebook=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
decodebook=(HashMap)((ObjectInputStream)为)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回此。decodebook=decodebook;
}
看来这本书会有用的。 关于codebook,我刚得到一个空值


我想知道是否有人能发现这个问题如果不是,谢谢你的尝试。

我自己在一个单独的程序中写出来,并像这样声明代码本; 它很有魅力,不回答为什么它不能在主程序中工作 今天我将继续攻击它。谢谢大家

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Runner {

Map<Integer, String> decodebook = new HashMap<Integer, String>();
// List<Integer> numbers = new LinkedList<Integer>();
Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
List<Integer> numbers = new LinkedList<Integer>();
List<Integer> otherNumbers = new LinkedList<Integer>();

public static void main(String[] args) throws IOException {
    new Runner();
}

public Runner() throws IOException {
    /*
     * numbers.add(56); numbers.add(16); numbers.add(36); numbers.add(36);
     * numbers.add(66); numbers.add(6);
     * 
     * otherNumbers.add(68); otherNumbers.add(78); otherNumbers.add(28);
     * otherNumbers.add(668); otherNumbers.add(618); otherNumbers.add(686);
     * otherNumbers.add(682);
     * 
     * 
     * codebook.put("alf", numbers); codebook.put("tony", otherNumbers);
     * decodebook.put(7898, "alf"); decodebook.put(87576, "tony");
     */

    // saveStuff();
    loadBooks();
}

public void loadBooks() throws IOException {

    loadCodeBook("CodeBook");
    System.out.println(codebook);

    loadDeCodeBook("DecodeBook");
    // System.out.println(codebook);
    System.out.println(decodebook);

}

public Map<String, List<Integer>> loadCodeBook(String filePath)
        throws IOException {
    HashMap<String, List<Integer>> codebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        codebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.codebook = codebook;
}

public Map<Integer, String> loadDeCodeBook(String filePath)
        throws IOException {
    HashMap<Integer, String> decodebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        decodebook = (HashMap<Integer, String>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.decodebook = decodebook;
}

/*
 * public void saveStuff(){ try { saveCodeBook(codebook, "CodeBook");
 * 
 * } catch (IOException e) { // TODO Auto-generated catch block
 * e.printStackTrace(); } try {
 * 
 * saveDecodeBook(decodebook, "DecodeBook"); } catch (IOException e) { //
 * TODO Auto-generated catch block e.printStackTrace(); }
 * 
 * }
 * 
 * public void saveCodeBook(Map<String, List<Integer>> obj, String filePath)
 * throws IOException { OutputStream os = null; try { os = new
 * ObjectOutputStream(new FileOutputStream(filePath));
 * 
 * ((ObjectOutputStream) os).writeObject(obj); } catch (Exception ex) { }
 * finally { os.close(); } }
 * 
 * public void saveDecodeBook(Map<Integer,String> obj, String filePath)
 * throws IOException { OutputStream os = null; try { os = new
 * ObjectOutputStream(new FileOutputStream(filePath));
 * 
 * ((ObjectOutputStream) os).writeObject(obj); } catch (Exception ex) { }
 * finally { os.close(); } }
 */

}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.io.OutputStream;
导入java.io.Serializable;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
公开课跑者{
Map decodebook=newhashmap();
//列表编号=新建LinkedList();
Map codebook=newhashmap();
列表编号=新建LinkedList();
List otherNumbers=新建LinkedList();
公共静态void main(字符串[]args)引发IOException{
新手();
}
公共运行程序()引发IOException{
/*
*numbers.add(56);numbers.add(16);numbers.add(36);numbers.add(36);
*编号。添加(66);编号。添加(6);
* 
*otherNumbers.add(68);otherNumbers.add(78);otherNumbers.add(28);
*otherNumbers.add(668);otherNumbers.add(618);otherNumbers.add(686);
*其他数字。增加(682);
* 
* 
*码本。put(“阿尔夫”,数字);码本。put(“托尼”,其他数字);
*decodebook.put(7898,“alf”);decodebook.put(87576,“托尼”);
*/
//saveStuff();
负荷书();
}
public void loadBooks()引发IOException{
载荷码本(“码本”);
System.out.println(码本);
loadDeCodeBook(“DecodeBook”);
//System.out.println(码本);
System.out.println(解码本);
}
公共地图加载码本(字符串文件路径)
抛出IOException{
HashMap码本=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
codebook=(HashMap)((ObjectInputStream)是)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回this.codebook=codebook;
}
公共地图loadDeCodeBook(字符串文件路径)
抛出IOException{
HashMap decodebook=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
decodebook=(HashMap)((ObjectInputStream)为)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回此。decodebook=decodebook;
}
/*
*public void saveStuff(){try{saveCodeBook(codebook,“codebook”);
* 
*}catch(IOE异常){//TODO自动生成的catch块
*e.printStackTrace();}试试看{
* 
*saveDecodeBook(decodebook,“decodebook”);}catch(IOE异常){//
*TODO自动生成的捕获块e.printStackTrace();}
* 
* }
* 
*公共void存储码本(映射对象,字符串文件路径)
*抛出IOException{OutputStream os=null;尝试{os=new
*ObjectOutputStream(新文件输出流(文件路径));
* 
*((对象)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Runner {

Map<Integer, String> decodebook = new HashMap<Integer, String>();
// List<Integer> numbers = new LinkedList<Integer>();
Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
List<Integer> numbers = new LinkedList<Integer>();
List<Integer> otherNumbers = new LinkedList<Integer>();

public static void main(String[] args) throws IOException {
    new Runner();
}

public Runner() throws IOException {
    /*
     * numbers.add(56); numbers.add(16); numbers.add(36); numbers.add(36);
     * numbers.add(66); numbers.add(6);
     * 
     * otherNumbers.add(68); otherNumbers.add(78); otherNumbers.add(28);
     * otherNumbers.add(668); otherNumbers.add(618); otherNumbers.add(686);
     * otherNumbers.add(682);
     * 
     * 
     * codebook.put("alf", numbers); codebook.put("tony", otherNumbers);
     * decodebook.put(7898, "alf"); decodebook.put(87576, "tony");
     */

    // saveStuff();
    loadBooks();
}

public void loadBooks() throws IOException {

    loadCodeBook("CodeBook");
    System.out.println(codebook);

    loadDeCodeBook("DecodeBook");
    // System.out.println(codebook);
    System.out.println(decodebook);

}

public Map<String, List<Integer>> loadCodeBook(String filePath)
        throws IOException {
    HashMap<String, List<Integer>> codebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        codebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.codebook = codebook;
}

public Map<Integer, String> loadDeCodeBook(String filePath)
        throws IOException {
    HashMap<Integer, String> decodebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        decodebook = (HashMap<Integer, String>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.decodebook = decodebook;
}

/*
 * public void saveStuff(){ try { saveCodeBook(codebook, "CodeBook");
 * 
 * } catch (IOException e) { // TODO Auto-generated catch block
 * e.printStackTrace(); } try {
 * 
 * saveDecodeBook(decodebook, "DecodeBook"); } catch (IOException e) { //
 * TODO Auto-generated catch block e.printStackTrace(); }
 * 
 * }
 * 
 * public void saveCodeBook(Map<String, List<Integer>> obj, String filePath)
 * throws IOException { OutputStream os = null; try { os = new
 * ObjectOutputStream(new FileOutputStream(filePath));
 * 
 * ((ObjectOutputStream) os).writeObject(obj); } catch (Exception ex) { }
 * finally { os.close(); } }
 * 
 * public void saveDecodeBook(Map<Integer,String> obj, String filePath)
 * throws IOException { OutputStream os = null; try { os = new
 * ObjectOutputStream(new FileOutputStream(filePath));
 * 
 * ((ObjectOutputStream) os).writeObject(obj); } catch (Exception ex) { }
 * finally { os.close(); } }
 */

}
codebook.put("tony", numbers.subList(0, 2));
codebook.put("alf", numbers.subList(3, 5));
codebook.put("alf", numbers);
codebook.put("tony", otherNumbers);