1

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

will-hart
  • 3,742
  • 2
  • 38
  • 48
Pendo826
  • 1,002
  • 18
  • 47

2 Answers2

2

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

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • Thanks man that worked. Also when i load the text file that im using in the IDE everything is in order. But when i open it in the project folder its all on one line. Any idea's? – Pendo826 Aug 06 '12 at 19:20
  • So when you open the file in your package explorer in your IDE, it shows up fine, but if you go to your system file explorer and open it, it has no line breaks? – Alex Coleman Aug 06 '12 at 19:22
  • Nope its all on one single line. it makes no sense because with the soloution you showed me its reading in 100% correct format :S – Pendo826 Aug 06 '12 at 19:24
  • 1
    Use .printLine I think, as windows uses the carriage return character (\r) but not all OS's do I think – Alex Coleman Aug 06 '12 at 20:03
2

If you're viewing the output in some text editor, it is possible it doesn't parse the '\n' as a line separator (for example in Notepad on Windows). Choose your way to resolve from this question.

Otherwise if this is your console output, file.readLine() stops at the newline, but doesn't add it to the string, so that's why it's all printed on one line. You will have to add the newline there yourself after each line.

Community
  • 1
  • 1
Humungus
  • 575
  • 2
  • 5
  • 16