프로그램이 처음 시작될때 obsave.txt  의 오브젝트 파일을 읽어서 바로 한줄게시판식으로 글을 남길수 있도록 했습니다.



현재 메인함수를 실행시켜 하는 모습... 처음에 3개의 게시물은 오브젝트에서 실행할때 자동으로 불러들여졌음.
       또한 3번 종료시에 자동으로 오브젝트 파일이 그대로 obsave.txt에 저장됨.





   코드는 다음과 같고, 실행을 직접 해보시려면 첨부파일을 다운받아 이클립스로 실행시키세요.

// BoardMain.java

package board; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class BoardMain { // private static final int POST = 1; private static final int WRITE = 1; private static final int DELET = 2; private static final int EXIT = 3; List<Post> posts = new ArrayList<Post>(); public static void main(String[] ar) { new BoardMain().board(); } private void board() { openPage(); while (true) { show(); menu(); } } private void openPage() { File fl = new File("Obsave.txt"); try { FileInputStream fis = new FileInputStream(fl); BufferedInputStream bis = new BufferedInputStream(fis); ObjectInputStream ois = new ObjectInputStream(bis); Object obj = null; posts.clear(); while (true) { try { obj = ois.readObject(); } catch (EOFException e) { break; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Post q = (Post) obj; posts.add(q); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void show() { int i = 1; System.out .println("┌───────┬──────────────────┬──────────────────────────────────"); System.out.println("│ no │ name │ story"); System.out .println("├───────┼──────────────────┼──────────────────────────────────"); for (Post a : posts) { System.out.println("│ " + i + " │ " + a.getName() + " │ " + a.getStory()); i++; } System.out .println("└───────┴──────────────────┴──────────────────────────────────"); } // public void printMain(){ // System.out.println(" " + Subject + " " + Name ); // } private void menu() { System.out.print("1. 글 쓰기 // 2. 글 삭제 // 3. 게시판 종료 ** Select : >> "); ActionCorrector(inputSelect()); } private void ActionCorrector(int inputSelect) { switch (inputSelect) { // case POST: // postread(); // break; case WRITE: postwrite(); break; case DELET: selectDelet(); break; case EXIT: obSave(); System.exit(0); break; } } private void obSave() { File fl = new File("Obsave.txt"); try { FileOutputStream fos; fos = new FileOutputStream(fl); BufferedOutputStream bos = new BufferedOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(bos); for (Post q : posts) { oos.writeObject(q); } oos.close(); } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } } private void selectDelet() { System.out.print("삭제할 번호를 써주세요 : >>"); Scanner sc = new Scanner(System.in); posts.remove(Integer.parseInt(sc.nextLine()) - 1); } // private void postread() { // // Scanner sc = new Scanner(System.in); // System.out.print("글 번호를 입력하세요 :"); // int select = sc.nextInt(); // // // } private void postwrite() { Scanner sc = new Scanner(System.in); Post ps = new Post(); System.out.println(); System.out.print("글쓴이 : "); ps.setName(sc.nextLine()); System.out.println(); System.out.print("내용 : "); ps.setStory(sc.nextLine()); System.out.println(); posts.add(ps); } private int inputSelect() { Scanner sc = new Scanner(System.in); return sc.nextInt(); } }

 //  BoardMain.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class BoardMain {
    // private static final int POST = 1;
    private static final int WRITE = 1;
    private static final int DELET = 2;
    private static final int EXIT = 3;
    List<Post> posts = new ArrayList<Post>();
 
    public static void main(String[] ar) {
        new BoardMain().board();
    }
 
    private void board() {
        openPage();
        while (true) {
            show();
            menu();
        }
 
    }
 
    private void openPage() {
        File fl = new File("Obsave.txt");
 
        try {
            FileInputStream fis = new FileInputStream(fl);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ObjectInputStream ois = new ObjectInputStream(bis);
            Object obj = null;
            posts.clear();
            while (true) {
                try {
                    obj = ois.readObject();
                } catch (EOFException e) {
                    break;
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Post q = (Post) obj;
                posts.add(q);
 
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    private void show() {
        int i = 1;
        System.out
                .println("┌───────┬──────────────────┬──────────────────────────────────");
        System.out.println("│  no │ name    │         story");
        System.out
                .println("├───────┼──────────────────┼──────────────────────────────────");
        for (Post a : posts) {
            System.out.println("│  " + i + " │ " + a.getName() + "    │ "
                    + a.getStory());
 
            i++;
        }
        System.out
                .println("└───────┴──────────────────┴──────────────────────────────────");
 
    }
 
    // public void printMain(){
    // System.out.println(" " + Subject + " " + Name );
    // }
 
    private void menu() {
        System.out.print("1. 글 쓰기 // 2. 글 삭제 // 3. 게시판 종료  ** Select : >> ");
 
        ActionCorrector(inputSelect());
    }
 
    private void ActionCorrector(int inputSelect) {
        switch (inputSelect) {
        // case POST:
        // postread();
        // break;
        case WRITE:
            postwrite();
            break;
        case DELET:
            selectDelet();
            break;
        case EXIT:
            obSave();
            System.exit(0);
            break;
        }
    }
 
    private void obSave() {
        File fl = new File("Obsave.txt");
        try {
            FileOutputStream fos;
            fos = new FileOutputStream(fl);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            ObjectOutputStream oos = new ObjectOutputStream(bos);
 
            for (Post q : posts) {
 
                oos.writeObject(q);
            }
            oos.close();
 
        } catch (FileNotFoundException e) {
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private void selectDelet() {
 
        System.out.print("삭제할 번호를 써주세요 : >>");
        Scanner sc = new Scanner(System.in);
        posts.remove(Integer.parseInt(sc.nextLine()) - 1);
    }
 
    // private void postread() {
    //   
    // Scanner sc = new Scanner(System.in);
    // System.out.print("글 번호를 입력하세요 :");
    // int select = sc.nextInt();
    //   
    //   
    // }
 
    private void postwrite() {
        Scanner sc = new Scanner(System.in);
        Post ps = new Post();
 
        System.out.println();
        System.out.print("글쓴이 : ");
        ps.setName(sc.nextLine());
        System.out.println();
        System.out.print("내용 : ");
        ps.setStory(sc.nextLine());
        System.out.println();
 
        posts.add(ps);
 
    }
 
    private int inputSelect() {
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }
}
 //  Post.java
package board;
 
import java.io.Serializable;
 
public class Post implements Serializable{
 
    String Name;
    String Story;
    String Subject;
 
    public void setName(String nextLine) {
        this.Name = nextLine;
       
    }
 
    public void setStory(String nextLine) {
        this.Story = nextLine;
    }
 
    public String getName() {
        return Name;
    }
 
    public String getStory() {
        return Story;
    }
 
 
}


 

코드 다운로드 : PostBoard.zip


저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License

'STUDY > Java' 카테고리의 다른 글

[JAVA] 자바 한줄 게시판  (0) 2010.09.05
[JAVA] 달력 출력  (0) 2010.01.14
[java] 자바 환경 변수 등록  (0) 2010.01.06

Comment



티스토리 툴바