230314
어려웠던 점
implements Serializable
Stream을 Object 형태로 이동하면서 오류가 발생했는데 어떻게 해결할지 몰라서 혼자 한참을 고민했다. 검색 후에는 해결책을 쉽게 찾을 수 있었다. ㅎㅎ.. 검색을 하자..
기존에 작성한 글이 서버가 꺼졌다 켜지더라도 동일하게 남아있음
https://github.com/leemyunga/leema0927.git Board4
private void readFile() throws IOException {
String path = "C:/img/temp/practice.dat";
File file = new File(path);
if(file.exists()) {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
try {
list = (ArrayList<BoardBean>) ois.readObject();
}catch (Exception e) {
e.printStackTrace();
}
ois.close();
System.out.println("저장된 파일을 읽어왔습니다. ");
}else {
list = new ArrayList<BoardBean>();
System.out.println("새로운 ArrayList<BoardBean>를 만들었습니다. ");
}
}
private void writeFile() throws IOException {
String path = "C:/img/temp/practice.dat";
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
System.out.println("게시글을 파일에 저장했습니다. ");
oos.flush();
oos.close();
}