2017-12-28 18 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ファイル
  • プログラムを持っている任意のコンピュータとインターネット接続
  • テキストファイルからのアクセスを可能にするオンラインサーバーを多くを必要としないローカルデバイスで動作します 追加

これは実際にどのように実装したいのか、どのような方法がニーズに最も適しているかによって異なります。

リーダライタを使用してフィールドを格納するには、各変数のアクセッサメソッドを使用してテキストファイルに1行ずつ格納することができます。以下は、ファイルへの書き込みを開始するためのサンプルコードです。

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(); 
関連する問題