2014-04-05 3 views
0

비슷한 질문이 게시되었습니다. 더러워지며 더 쉽게 따라 할 수 있습니다. Runner()에있는/부분은 /입니다. 이 주석을 해제하고 프로그램을 한 번 실행 한 다음 콘솔 출력을 확인하십시오. 그런 다음/은 다시으로 주석 처리하고 프로그램을 두 번 실행하면 subCodebook = {}이 공백/null을 반환한다는 것을 알 수 있습니다. 이것은 내 질문입니다, 왜 이것이 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

하위 코드북을 저장하고로드하지 않았습니다. – drkunibar

+0

@drkunibar 위 코드가 바뀌었고 예상대로 null이되었습니다. 이전에 null을 반환하지 않는 이유가 궁금했습니다. 이제는 출력이됩니다. 코드북 {alf = [6, 156, 363, 336, 26, 61], 톤 = [68,78,28,668,818,686,682] 디코드 북 {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

우수를 봐, 그 치료를 작동, 그 치료는 메인 프로그램에 양고기 의류에 늑대입니다. 많은 리팩토링이 필요합니다. 아직도 당신의 솔루션으로 그것은 내가 처음 예상했던 것만 큼 나쁘지 않다는 것을 보여 주었고, 나는 약간의 리팩터링으로 그것을 해결할 수 있다고 확신한다. – user3459475

+0

내가 도와 줄 수있는 좋은 것을 즐겨라. – drkunibar

+0

그것은 대단원이다. 고맙다. 여기에 붙어있을 때 도움을 얻을 수 있다는 사실을 사랑합니다, 그것은 긴 과정이었습니다. 프로그램 운영을 지켜 보는 것은 즐거웠습니다. 다시 한 번 감사드립니다. – user3459475