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 8352893
In Process

The Archive Base Latest Questions

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

I’m using RandomAccessFile to create a database to a text file. Basically I created

  • 0

I’m using RandomAccessFile to create a database to a text file. Basically I created a normal store using an ArrayList and now i need to output the contents of the store using RandomAccessFile. But I am stuck on how to get the randomAccessFile method to take the student store. Here is my code:

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
{
    private RandomAccessFile File;

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




        //Create a file object.
        File contactDetailsFile = new File("StudentDetails.txt");
        //Open a buffered output stream to allow write to file operations.
        PrintWriter out = new PrintWriter(
                          new BufferedWriter(
                          new FileWriter(contactDetailsFile)));   

        out.println(a);
        out.println(b);
        out.println(c);
        out.println(d);
        out.println(e);
        out.close();

        BufferedReader in = new BufferedReader(
                            new FileReader(contactDetailsFile));

        String line = in.readLine();
        System.out.println(line);

        out.close();

    }
/**
 * @param args the command line arguments
 */
public void RandomAccessFile(String filename) 
{

    RandomAccessFile randomAccessFile = null;
    try {

        //Declare variables that we're going to write
        String line1 = "First line\n";
        String line2 = "Second line\n";

        //Create RandomAccessFile instance with read / write permissions
        randomAccessFile = new RandomAccessFile(filename, "rw");

        //Write two lines to the file
        randomAccessFile.writeBytes(line1);
        randomAccessFile.writeBytes(line2);

        //Place the file pointer at the end of the first line
        randomAccessFile.seek(line1.length());

        //Declare a buffer with the same length as the second line
        byte[] buffer = new byte[line2.length()];

        //Read data from the file
        randomAccessFile.read(buffer);

        //Print out the buffer contents
        System.out.println(new String(buffer));

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {

            if (randomAccessFile != null)
                randomAccessFile.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}   

}
  • 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-09T09:06:08+00:00Added an answer on June 9, 2026 at 9:06 am

    In a word, yes.

    You will, however, have to re-do your write-to-file part of this solution again. You must read objects back out the same way you wrote them.

    From this example, you can see how each member is written using the RandomAccessFile object.

    I would suggest that you make the read method from the example return a new Student object.

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.