Question

I am making an application that stores student details in a file using RandomAccessFile but the only way that i have achieved this is by adding the store students a,b,c,d,e etc... into a static array and using a getBytes method to write it. The store is made using an arrayList. I have tried many things and many methods and i cant figure it out without using the static array.

Here is my code:

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

}

Note: I havent shown the student class because it is built up of a constructor, getters and setters and a toString and i didnt feel the use of uploading it but if needed i will gladly do it.

Was it helpful?

Solution

You can mark Student as implementing Serializable since it looks to be just storing a bunch of text. Then you can get the bytes out of your List<Student> students to store like so:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(students);
out.close();

byte[] bytes = bos.toByteArray();

you could then extract it back from the file by reading the bytes in the file into a byte array and then doing:

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
students = (List<Student>) in.readObject();
in.close();

Edit: just writing the Student.toSting() as bytes you could do:

public void write(RandomAccessFile file) throws IOException
{
    for (Student s: students)
    {
        byte[] bytes = s.toString().getBytes();
        for(byte byteWrite : bytes)
        {
            file.writeByte(byteWrite);
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top