1

I'm using random access files to write a raf using an arrayList store. I do not know if it can be done, but I'm giving it a try because it is the best solution for me to create this application.

Here is the run-time error that I am getting:

Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readChar(Unknown Source)
at Student.read(Student.java:93)
at MainApp.start(MainApp.java:60)
at MainApp.main(MainApp.java:17)

And here is my code:

MainApp
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    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");




        //details.print();



         RandomAccessFile raf = new RandomAccessFile("ContactDetails.txt", "rw");

         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);
            for (int i = 0; i < details.size(); i++) 
            {
              //a.setStudentName(a[i]);
              //a.setLastName(lnames[i]);
             // a.setAddress(addresses[i]);
             // a.setAge(ages[i]);
             // a.setSalary(salaries[i]);
              a.write(raf);
            }
            raf = new RandomAccessFile("employee.dat", "rw");

           // er = new Student();

            int numRecords = (int) raf.length() / details.size();

            for (int i = 0; i < numRecords; i++) {
              a.read(raf);

              System.out.print(a.getStudentName() + " ");
              System.out.print(a.getStudentId() + " ");
              System.out.print(a.getStudentEmail() + " ");
              System.out.print(a.getStudentTelephoneNumber() + " ");
            }
            raf.seek(0);
            for (int i = 0; i < numRecords; i++) 
            {
              a.read(raf);
                raf.seek(raf.getFilePointer() - details.size());
                a.write(raf);
                raf.seek(raf.getFilePointer() - details.size());
                a.read(raf);
              }
              System.out.print(a.getStudentName() + " ");
              System.out.print(a.getStudentId() + " ");
              System.out.print(a.getStudentEmail() + " ");
              System.out.print(a.getStudentTelephoneNumber() + " ");
            }

          }

Student

import java.io.IOException;
import java.io.RandomAccessFile;


public class Student 
{
//---------------------------------------------------------------------------
//  Class Variables.
//---------------------------------------------------------------------------   
    private String studentName;
    private String studentId;
    private String studentTelephoneNumber;
    private String studentEmail;
//---------------------------------------------------------------------------
//  Constructor.
//---------------------------------------------------------------------------   
    public Student(String studentName, String studentId,String studentTelephoneNumber, String studentEmail) 
    {
        this.studentName = studentName;
        this.studentId = studentId;
        this.studentTelephoneNumber = studentTelephoneNumber;
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------
//  Getters.
//---------------------------------------------------------------------------
    public String getStudentName()
    {
        return studentName;
    }
    public String getStudentId() 
    {
        return studentId;
    }
    public String getStudentTelephoneNumber() 
    {
        return studentTelephoneNumber;
    }
    public String getStudentEmail() 
    {
        return studentEmail;
    }
//---------------------------------------------------------------------------
//  Setters.
//---------------------------------------------------------------------------
    public void setStudentName(String studentName) 
    {
        this.studentName = studentName;
    }
    public void setStudentId(String studentId) 
    {
        this.studentId = studentId;
    }
    public void setStudentTelephoneNumber(String studentTelephoneNumber) 
    {
        this.studentTelephoneNumber = studentTelephoneNumber;
    }
    public void setStudentEmail(String studentEmail) 
    {
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------
//  toString.
//---------------------------------------------------------------------------
    public String toString() 
    {
        return "---------------------------Student--------------------------- " +
                "\nStudent Name:" + studentName + 
                "\nStudent Id:"+ studentId + 
                "\nStudent Telephone Number:"+ studentTelephoneNumber + 
                "\nStudent Email:" + studentEmail;
    }
    void read(RandomAccessFile raf) throws IOException 
    {
        char[] temp = new char[15];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentName = new String(temp);
        temp = new char[15];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentId = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
          temp[i] = raf.readChar();
        studentEmail = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
              temp[i] = raf.readChar();
        studentTelephoneNumber = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
              temp[i] = raf.readChar();
      }

      void write(RandomAccessFile raf) throws IOException 
      {
        StringBuffer sb;
        if (studentName != null)
          sb = new StringBuffer(studentName);
        else
          sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (studentId != null)
          sb = new StringBuffer(studentId);
        else
          sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (studentEmail != null)
          sb = new StringBuffer(studentEmail);
        else
          sb = new StringBuffer();

        sb.setLength(30);
        raf.writeChars(sb.toString());
        if (studentTelephoneNumber != null)
              sb = new StringBuffer(studentTelephoneNumber);
            else
              sb = new StringBuffer();

            sb.setLength(30);
            raf.writeChars(sb.toString());

      }



}
Ben
  • 51,770
  • 36
  • 127
  • 149
Pendo826
  • 1,002
  • 18
  • 47

3 Answers3

2

You are reading beyond the end of the file. Reading data that isn't there.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

The basic problem is that you are reading more data than you are writing.

You are reading 30 char at the end of each record which you didn't write. Given you discard them it appears you don't need to be doing this. I would delete the code which reads after studentTelephoneNumber

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I deleted //temp = new char[30]; //for (int i = 0; i < temp.length; i++) // temp[i] = raf.readChar(); But i still get the error. – Pendo826 Aug 06 '12 at 12:51
  • How does that make sense. There must be an alternate problem. – Pendo826 Aug 06 '12 at 12:56
  • Perhaps you are not running the code you think you are, try doing a clean build. – Peter Lawrey Aug 06 '12 at 12:59
  • When I run the code I see `Becky O'Brien DKIT26 bexo@hotmail.com 0876126944` is that not what you expected? – Peter Lawrey Aug 06 '12 at 13:01
  • Yes that is one of the students that needs to output. There is also other students not showing. But apart from that Becky is printing out and then that error is shown. – Pendo826 Aug 06 '12 at 13:10
  • That line is the last thing the program does for me. BTW: I suspect the first and second file names should be the same which is why most of your code doesn't do any thing in my case ;) – Peter Lawrey Aug 06 '12 at 13:15
  • Im just goin through it step by step. This is my first time doing raf. So i suspect there is a lot of tweeks needed. – Pendo826 Aug 06 '12 at 13:17
  • 1
    I suspect you are reading an old copy of the file I don't have. ;) – Peter Lawrey Aug 06 '12 at 13:18
  • I actually do have an old file. I will try renaming the file. – Pendo826 Aug 06 '12 at 13:20
  • Why not read the file you just wrote so it has something in it?? ;) – Peter Lawrey Aug 06 '12 at 13:22
  • Just tried that i have no idea where this error is coming from. I think i will start it again and run it step at a time. Can i ask you is it anything to do with the fact that im reading/writing from a store? – Pendo826 Aug 06 '12 at 13:25
  • 1
    No. It is more likely that you are a) reading from an old file b) using an old version of the build. If you start with a clean file and make sure the build is up to date you shouldn't get an error as I don't with the code you posted. – Peter Lawrey Aug 06 '12 at 13:30
0

If load just one detail do you get the problem? I think your problem is that you are reading off the end of the file. Your read function which reads 15/30 characters seem dangerous because you may not have written that much. Why not read until you get a comma or some other separator? (eg tab)

It's this sort of thing that looks dangerous

char[] temp = new char[15]; 
for (int i = 0; i < temp.length; i++) 
    temp[i] = raf.readChar(); 

Where does the 15 come from?

Try and break it down with just one detail and bulid up from there. You should find the error. I would personally read until you find a particular separator.

RNJ
  • 15,272
  • 18
  • 86
  • 131
  • Your correct about the one detail. That shows no errors. I will do what you suggest thanks. – Pendo826 Aug 06 '12 at 12:58
  • keep trying the different details until you find which detail is the problem. I suspect this is one where there are fewer than 15 characters in the name. This is the problem with usuing magic number. Try and use a separator. You can read in the whole line of the file and then split it up using String.split(). this will give you an array of the different parts of the line – RNJ Aug 06 '12 at 13:02