비슷한 질문이 게시되었습니다. 더러워지며 더 쉽게 따라 할 수 있습니다. 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();
}
}
}
하위 코드북을 저장하고로드하지 않았습니다. – drkunibar
@drkunibar 위 코드가 바뀌었고 예상대로 null이되었습니다. 이전에 null을 반환하지 않는 이유가 궁금했습니다. 이제는 출력이됩니다. 코드북 {alf = [6, 156, 363, 336, 26, 61], 톤 = [68,78,28,668,818,686,682] 디코드 북 {87576 = tony, 7898 = alf} subCodebooknull – user3459475
그래 알았어. 내 답변 – drkunibar