2012-08-07 49 views
0

嘿,我被困在我的ArrayList存儲學生的add方法,然後使用randomAccessFile將學生存儲寫入文本文件。所以我需要能夠將商店添加到商店,然後將其寫入文件。任何人都可以提出一種方法,我可以做到這一點?我可以將學生添加到商店沒有問題,但實際的問題是,我正在使用靜態數組將學生寫入文件,並且我不知道如何讓用戶將新員工添加到靜態數組中。爲RandomAccessFile添加方法

這裏是我的代碼:

MainApp

import java.io.IOException; 
import java.io.RandomAccessFile; 
public class MainApp 
{ 

    public static void main(String[] args) throws Exception 
    { 
     new MainApp().start(); 

    } 
    public void start()throws Exception 
    { 
     StudentStore details = new StudentStore(); 
     Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "[email protected]"); 
     Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "[email protected]"); 
     Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "[email protected]"); 
     Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "[email protected]"); 
     Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "[email protected]"); 
     details.add(a); 
     details.add(b); 
     details.add(c); 
     details.add(d); 
     details.add(e); 
     //details.print(); 


     RandomAccessFile file = new RandomAccessFile("ContactDetails.txt","rw"); 
     //getBytes() returns an array of bytes. 
     //Because i have put the store in a static Array.(I done this because i could find no other 
     //Simple way to write a Student Object.) 
     //None of the methods of the RandomAccessFile write class worked with this. 
     Student[] students = {a,b,c,d,e}; 
     details.write(students, file); 
     details.readAll(file); 



     file.close(); 


    } 


} 

StudentStore

//--------------------------------------------------------------------------- 
//Imports. 
//--------------------------------------------------------------------------- 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.util.ArrayList; 
import java.util.List; 
//--------------------------------------------------------------------------- 

public class StudentStore 
{ 
//--------------------------------------------------------------------------- 
//ArrayList declaration. 
//--------------------------------------------------------------------------- 
    List<Student> students = new ArrayList<Student>(); 
//--------------------------------------------------------------------------- 
//Name:   Add method. 
//Description: Adds a student to the ArrayList. 
//--------------------------------------------------------------------------- 
    public void add(Student student) 
    { 
     students.add(student); 
    } 
//--------------------------------------------------------------------------- 
//Name:   DeleteAll method. 
//Description: Delete's everything in the ArrayList. 
    //--------------------------------------------------------------------------- 
    public void deleteAll() 
    { 
      students.clear(); 
    } 
//--------------------------------------------------------------------------- 
//Name:   Print method. 
//Description: Prints out the contents of the ArrayList. 
//--------------------------------------------------------------------------- 
    public void print() 
    { 
     for (int i = 0; i < students.size(); i++) 
     { 
      Student a = students.get(i); 
      System.out.println(a.toString()); 
     } 
    } 
    public int size() 
    { 
     return (students == null) ? 0 : students.size(); 
    } 
    public void write(Student[] students, RandomAccessFile file) throws IOException 
    { 
     for (int i = 0; i < students.length; i++) 
     { 
     byte[] bytes = students[i].toString().getBytes(); 
     for(byte byteWrite : bytes) 
     { 
      file.writeByte(byteWrite); 
     } 
     } 

    } 
    public void readAll(RandomAccessFile file) throws IOException 
    { 
     final int Record_Length = 30; 
     int recordNumber = 0; 
     file.seek((recordNumber) * Record_Length); 

     String code =""; 
     for(int i = 0; i < 30; i++) 
     { 
     code += file.readLine() + "\n"; 
     } 
     System.out.println(code); 
    } 

} 

注:我還沒有出的學生類,因爲它是建立了一個構造函數,getter和setter和一個toString,我不覺得使用上傳,但如果需要,我會很樂意做到這一點。

+1

在你的寫入方法中,你應該迭代ArrayList而不是傳入一個數組作爲參數嗎? – 2012-08-07 21:18:58

+0

Im作爲參數傳入數組,以便我可以在mainApp中使用該方法,而不需要在main的底部有很多方法。 – Pendo826 2012-08-07 21:28:27

+1

我可能會使用靜態'List'而不是數組。你「可以」看看'Arrays'類http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html,因爲它有複製數組的方法以及'System.copyArray 'http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int ),但說實話,'ArrayList'無論如何都會爲你做這個 – MadProgrammer 2012-08-07 21:34:21

回答

1

就我個人而言,我只是將數組列表寫入文件而不是創建一個單獨的數組,以便將寫入方法寫入文件。這也會簡化你的主要方法。你可以看到一個很好的例子here

所以,你的主要會是這個樣子:

StudentStore details = new StudentStore(); 
details.add(new Student("Becky O'Brien", "DKIT26", "0876126944", "[email protected]")); 
// add more students 
details.write(new RandomAccessFile("ContactDetails.txt","rw")); 

您還可以進一步簡化它,只是有write方法的唯一參數是一個文件名。

+0

我在寫學生時遇到了麻煩,所以我使用數組工作。 – Pendo826 2012-08-08 11:39:02