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

The Archive Base Latest Questions

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

Hey Im writing to a text file using file i/o using an array of

  • 0

Hey Im writing to a text file using file i/o using an array of Students from an ArrayList store. Thats all well and good. I have that working. But in the actually text file, the details are printing and reading in a single line.

Can anyone see the problem.

Here is my code:

MainApp

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};
        for (int i = 0; i < students.length; i++)
        {
        byte[] bytes = students[i].toString().getBytes();
        for(byte byteWrite : bytes)
        {
            file.writeByte(byteWrite);
        }
        }
        final int Record_Length = 30;
        int recordNumber = 1;
        file.seek((recordNumber) * Record_Length);

        String code ="";
        for(int i = 0; i < 4; i++)
        {
        code += file.readLine();
        }
        System.out.println(code);
        file.close();


     }


 }

ToString

//---------------------------------------------------------------------------
//  toString.
//---------------------------------------------------------------------------
    public String toString() 
    {
        return "---------------------------Student--------------------------- " +
                "\nStudent Name:" + studentName + 
                "\nStudent Id:"+ studentId + 
                "\nStudent Telephone Number:"+ studentTelephoneNumber + 
                "\nStudent Email:" + studentEmail +"\n\n";
    }

The output
dent————————— Student Name:Becky O’BrienStudent Id:DKIT26Student Telephone Number:0876126944

  • 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-09T03:50:25+00:00Added an answer on June 9, 2026 at 3:50 am

    It is not the writing that is messing up I think;

        String code ="";
        for(int i = 0; i < 4; i++)
        {
        code += file.readLine();
        }
        System.out.println(code);
    

    When you call readLine, it gets the current line, but you have to add the line break (\n) in yourself, so it should be file.readLine() + “\n” instead of just file.readLine()

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

Sidebar

Related Questions

Hey all, I'm writing a C program, and I want to have an array
Hey guys, I'm writing a word wrap function to format console text in C++.
Hey I am writing an app in Twisted, and as it stands I have
Hey I am writing a function that handles favorites within my app, as well
Hey all, quick NHibernate question. In my current project, we have a denormalized table
Hey, I'm currently trying to send an image file to a web server using
Hey. i am writing a forum, and i have this table that marks messages
Hey guys, I'm writing a Firefox plugin and using the following function: var obj
Problem I have a file which contains such text (nothing else) 2010-10-05 I would
Hey guys! I need some help writing a code that creates an array in

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.