0

ねえ、私はArrayListストアの学生用のaddメソッドで立ち往生しています。このメソッドは、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", "bexo@hotmail.com");
        Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "fabioborini@gmail.com");
        Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "gramirez@webmail.com");
        Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "luissuarez@yahoo.com");
        Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "carroll123@hotmail.com");
        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);
    }

}

注:コンストラクター、ゲッターとセッター、およびtoStringで構成されているため、学生クラスは表示していません。アップロードの使用感は感じませんでしたが、必要に応じて喜んで実行します。

4

1 に答える 1

1

個人的には、別の配列を作成してwriteメソッドをファイルに書き込むのではなく、arraylistをファイルに書き込むだけです。これにより、メインの方法も簡素化されます。ここで良い例を見ることができます

したがって、メインは次のようになります。

StudentStore details = new StudentStore();  
details.add(new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com"));  
// add more students  
details.write(new RandomAccessFile("ContactDetails.txt","rw"));

さらに単純化して、writeメソッドの唯一のパラメーターをファイル名にすることもできます。

于 2012-08-08T03:28:35.063 に答える