Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:44:26+00:00 2026-06-09T04:44:26+00:00

I’m using random access files to write a raf using an arrayList store. I

  • 0

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

      }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:44:28+00:00Added an answer on June 9, 2026 at 4:44 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.