Search code examples
javarandom-access

RandomAccessFile printing to text file trouble


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


Solution

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