添加RandomAccessFile的方法

时间:2012-08-07 20:08:45

标签: java random-access

嘿,我被困在我的ArrayList商店学生的add方法上,然后学生使用randomAccessFile将学生商店写入文本文件。所以我需要能够将学生添加到商店然后将其写入文件。任何人都可以建议我可以这样做吗?我可以将学生添加到商店没有问题,但实际问题是我正在使用静态数组将学生写入文件,我不知道如何让用户将新员工添加到静态数组? / p>

这是我的代码:

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);
    }

}

注意:我没有展示学生班,因为它是由构造函数,getter和setter以及toString组成的,我没有感觉使用上传它,但如果需要,我很乐意这样做。

1 个答案:

答案 0 :(得分:1)

就个人而言,我只是将arraylist写入文件而不是创建一个单独的数组,以使您的write方法写入文件。这也将简化您的主要方法。你可以看到一个很好的例子here

所以,你的主要看起来像这样:

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方法的唯一参数设置为文件名。