2014-04-05 40 views
0

我有一個類似的問題發佈,它變得混亂,這將是更容易遵循。在Runner()中的部分是/ 註釋掉了 /。你取消註釋,運行程序一次,檢查你的控制檯打印出來。然後/ 再次註釋掉 /並再次運行該程序,您會注意到subCodebook = {}正在返回一個空白/空值;這是我的問題,爲什麼這返回null。問題保存和加載到/從Java文件

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>>(); 
Map<String, List<Integer>> subcodebook = 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(6); 
    numbers.add(156); 
    numbers.add(363); 
    numbers.add(336); 
    numbers.add(26); 
    numbers.add(61); 

    otherNumbers.add(68); 
    otherNumbers.add(78); 
    otherNumbers.add(28); 
    otherNumbers.add(668); 
    otherNumbers.add(618); 
    otherNumbers.add(686); 
    otherNumbers.add(682); 

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


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

    saveStuff();*/ 


    loadBooks(); 
    System.out.println(" codebook" + codebook); 
    System.out.println(" decodebook" + decodebook); 
    System.out.println(" subCodebook" + subcodebook); 
} 

public void loadBooks() throws IOException { 

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

    loadsubCodeBook("subCodeBook"); 


    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<String, List<Integer>> loadsubCodeBook(String filePath) 
     throws IOException { 
    HashMap<String, List<Integer>> subcodebook = null; 
    InputStream is = null; 
    try { 
     is = new ObjectInputStream(new FileInputStream(filePath)); 
     subcodebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is) 
       .readObject(); 
    } catch (Exception ex) { 
    } finally { 
     is.close(); 
    } 
    return this.subcodebook = subcodebook; 
} 


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

     savesubCodeBook(subcodebook, "subcodeBook"); 
    } 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 savesubCodeBook(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(); 
    } 
} 

}

+0

您沒有保存並加載subCodebook。 – drkunibar

+0

@drkunibar我改變了上面的代碼,並如預期的那樣,它現在爲空,我想知道爲什麼它沒有返回null。所以現在打印出來了。 codebook {alf = [6,156,363,336,26,61],tony = [68,78,28,668,618,686,682]} decodebook {87576 = tony,7898 = alf} subCodebooknull – user3459475

+0

好的我明白了。看看我的回答 – drkunibar

回答

0

YOUT儘量節省(和恢復)同樣的對象兩次。試試這個便條the important part

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.ArrayList; 
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>>(); 
    Map<String, List<Integer>> subcodebook = 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(6); 
     numbers.add(156); 
     numbers.add(363); 
     numbers.add(336); 
     numbers.add(26); 
     numbers.add(61); 

     otherNumbers.add(68); 
     otherNumbers.add(78); 
     otherNumbers.add(28); 
     otherNumbers.add(668); 
     otherNumbers.add(618); 
     otherNumbers.add(686); 
     otherNumbers.add(682); 

     // --------------------------------------------------------- 
     // 
     // the imprtant part 
     // 
     // ------------------------------------------------------------ 
     subcodebook.put("tony", new ArrayList(numbers.subList(0, 2))); 
     subcodebook.put("alf", new ArrayList(numbers.subList(3, 5))); 

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

     saveStuff(); 
     System.out.println(" codebook" + codebook); 
     System.out.println(" decodebook" + decodebook); 
     System.out.println(" subCodebook" + subcodebook); 
     loadBooks(); 
     System.out.println(" codebook" + codebook); 
     System.out.println(" decodebook" + decodebook); 
     System.out.println(" subCodebook" + subcodebook); 
    } 

    public void loadBooks() throws IOException { 

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

     loadsubCodeBook("subcodeBook"); 

     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<String, List<Integer>> loadsubCodeBook(String filePath) 
      throws IOException { 
     HashMap<String, List<Integer>> subcodebook = null; 
     InputStream is = null; 
     try { 
      is = new ObjectInputStream(new FileInputStream(filePath)); 
      subcodebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is) 
        .readObject(); 
     } catch (Exception ex) { 
     } finally { 
      is.close(); 
     } 
     return this.subcodebook = subcodebook; 
    } 

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

      savesubCodeBook(subcodebook, "subcodeBook"); 
     } 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 savesubCodeBook(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(); 
     } 
    } 

} 
+0

excelennt,那是一個款待,只爲了主節目只爲羔羊服裝的狼。很多重構都要完成。儘管如此,它已經告訴我它並沒有我第一次預期的那麼糟糕,但我確信我可以通過一些重構來解決它,謝謝 – user3459475

+0

享受 - 很好,我可以幫助 – drkunibar

+0

它是一種享受,非常感謝你,我確實喜歡在卡住時得到幫助的事實,那是一個漫長的過程。看着程序運行是一種快樂,所以再次感謝你。 – user3459475