2017-12-28 7 views
0

다음은 Student 객체의 내 생성자입니다. 나는 학생 목록을 사용할 것이다. 목록을 저장해야 프로그램이 꺼져 있어도 모든 내용에 계속 액세스 할 수 있습니다. 내가 생각할 수있는 유일한 방법은 리더/라이터와 텍스트 파일을 사용하는 것이 었습니다.여러 필드에 대해 작성기/판독기 사용

1)이 정보를 저장하는 더 효율적인 방법이 있습니까?
2) 그렇지 않은 경우 어떻게 리더/라이터를 사용하여 각 필드를 저장할 수 있습니까?

public Student(String firstName, String lastName, String gender, String 
state, String school, String lit, String wakeUp, String sleep, String 
social,String contactInfo, String country, String major) { 
this.firstName = firstName; 
this.lastName = lastName; 
this.gender = gender; 
this.state = state; 
this.school = school; 
this.lit = lit; 
this.wakeUp = wakeUp; 
this.sleep = sleep; 
this.social = social; 
this.contactInfo = contactInfo; 
this.country = country; 
this.major = major; 
} 

답변

0

가능성은 실제로 프로젝트와 주관적입니다. 일부 가능성은 다음과 같습니다

    데이터에게 프로그램 및 인터넷 연결
  • 텍스트 파일이있는 모든 컴퓨터에서 액세스 할 수 있습니다
  • 온라인 서버를 다른 프로그램에 수출 및 구문 분석이 쉽게
  • CSV 파일 많은 것을 필요로하지 않는 로컬 장치에서 작동합니다. 추가 사항

실제로는 구현 방법과 원하는 방법에 따라 다릅니다.

리더/라이터를 사용하여 필드를 저장하려면 각 변수의 접근 자 메서드를 사용하여 텍스트 파일에 한 줄씩 저장합니다. 다음은 파일에 쓰기 시작할 때 사용할 수있는 몇 가지 샘플 코드입니다.

PrintWriter outputStream = null; 

    try { 
     outputStream = new PrintWriter(new FileOutputStream(FILE_LOCATION)); 
    } 
    catch (FileNotFoundException ex) { 
     JOptionPane optionPane = new JOptionPane("Unable to write to file\n " + FILE_LOCATION, JOptionPane.ERROR_MESSAGE); 
     JDialog dialog = optionPane.createDialog("Error!"); 
     dialog.setAlwaysOnTop(true); 
     dialog.setVisible(true); 
     System.exit(0); 
    } 

    Iterator<YOUR_OBJECT> i = this.List.iterator(); 
    YOUR_OBJECT temp = null; 
    while (i.hasNext()) { 
     temp = i.next(); 
     if (temp instanceof YOUR_OBJECT) { 
      outputStream.println(temp.getAttribute()); 
     } 
    } 
    outputStream.close(); 
관련 문제