Question

I have two maps that get generated just fine. Now I want to stop generating them and work from a file.

The map structure is

private Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
private Map<Integer, String> decodebook = new HashMap<Integer, String>();

Below is the code I am using for saving and loading. The decodebook seems to work just fine, it is the codebook I can not seem to load back to program. I am afraid seen as I am working with a Objects it is difficult to tell if it is even saving correctly.

I run the program once and this code will run be run with it.

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();
    }
}

The second time I run the program, I comment out above code. (NOT THIS Piece directly below, this is always left)

private Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
private Map<Integer, String> decodebook = new HashMap<Integer, String>();

Ok once above code is removed, I run the program again, with this code included.

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;
}

It seems the decodebook will work. For about codebook I just get returned a null.

I was wondering if anyone can spot the problem? & if not thanks for trying.

Was it helpful?

Solution 3

I THOUGHT THIS WAS BEGINNING TO GET A BIT BUSY SO POSTED A MORE RESOLVED VERSION OF THIS QUESTION, THINGS WHERE LEARNED HERE, AND THESE THINGS HAVE GREW TO NEW QUESTION Issue Saving and Loading to/from File in Java

OTHER TIPS

I wrote it out myself in a separate program, and declared the codebook like so; it works a charm, and answering nothing to why it isn't working in main program I will continue hacking at it today. Thanks all.

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(); } }
 */

}

It seems this the sublist method is to blame. In the full program, I populate the codebooks List with the use of subsisting from a "List keyOne = linkedlist.sublist" For some reason, when it is loaded this way it causes problems in saving. Even in example below this post I added this in the Runner() method.

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

instead of

codebook.put("alf", numbers);
codebook.put("tony", otherNumbers);

And when I use sublist I get back null. Problem solved , only now a much bigger problem, sadly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top