0

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.

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Pendo826
  • 1,002
  • 18
  • 47
  • Another way to write the students from the ArrayList store without using the static array that i have shown above. – Pendo826 Aug 08 '12 at 22:50
  • why can't you just use the students array that is inside StudentStore? Why do you have to pass in that array? – jeff Aug 08 '12 at 22:58
  • Because none of the methods in RandomAccessFile allow an ArrayList to be passed in as a parameter. – Pendo826 Aug 08 '12 at 23:07

1 Answers1

1

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

}
SamYonnou
  • 2,068
  • 1
  • 19
  • 23
  • I cant get it working like that man. Is there anyway to let the user add students to the Arraylist and then the static array to print it out? – Pendo826 Aug 08 '12 at 23:06
  • 1
    Well what you're doing now is `students[i].toString().getBytes()` so with the `ArrayList` you could just do `students.get(i).toString().getBytes()` for the same result. (see my edit) – SamYonnou Aug 08 '12 at 23:10
  • 1
    I mean in your write method. Try using the `write` method i posted in the answer's edit and in your `start()` method just do `details.write(file)` – SamYonnou Aug 08 '12 at 23:16
  • 1
    Oh ok cool. Also `RandomAccessFile` has a `write(byte[])` method so you don't have to go over your `byte` array in a loop and do `writeByte(byte)` individually I think. – SamYonnou Aug 08 '12 at 23:19