0

having issues with my output while trying to create an array file

import java.io.*;
import java.util.ArrayList;

public class MainClass
{

    public static void main(String[] args)throws FileNotFoundException, IOException 
    {
        File myfile = new File("students.txt");
        ArrayList<Student> studentList = new ArrayList<Student>(); 
        studentList.add(new Student("Gondi", 68));
        studentList.add(new Student("George", 52));
        studentList.add(new Student("Aaron", 25));
        studentList.add(new Student("Gavin", 36));
        studentList.add(new Student("joe", 42));

        FileOutputStream fo = new FileOutputStream(myfile);
         ObjectOutputStream output = new ObjectOutputStream(fo);
         for (Student s : studentList)
         {
         output.writeObject(s);
         }
            fo.close();
            output.close();
    }

}

import java.io.Serializable; 
public class Student implements Serializable { 
    public String StudentName; 
    public int StudentAge; 
    public Student(String name, int age) { S
          tudentName = name; StudentAge = age; 
    } 
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45

3 Answers3

0

You are using ObjectOutputStream well try this one. Tested to perfection !

import java.io.*;
import java.util.ArrayList;
import java.io.Serializable; 

public class stackNotepad
{

    public static void main(String[] args)throws FileNotFoundException, IOException 
    {
        File myfile = new File("students.txt");
        ArrayList<Student> studentList = new ArrayList<Student>(); 
        studentList.add(new Student("Gondi", 68));
        studentList.add(new Student("George", 52));
        studentList.add(new Student("Aaron", 25));
        studentList.add(new Student("Gavin", 36));
        studentList.add(new Student("joe", 42));

        BufferedWriter writer = null;
        try
        {
            writer = new BufferedWriter( new FileWriter( myfile));
            writer.write(String.format("NAMES %15s","AGES"));
            writer.newLine();
            for (Student s : studentList)
            {
                writer.write(s.toString());
                writer.newLine();
            }
        }
        catch ( IOException e){}
        finally
        {
            try
            {
                if ( writer != null)
                    writer.close( );
            }
            catch ( IOException e)
            {}
        }
    }

}

class Student implements Serializable 
{ 
    public String StudentName; 
    public int StudentAge; 
    public Student(String name, int age)
    { 
        StudentName = name; StudentAge = age; 
    } 

    @Override
    public String toString()
    {
        return String.format("%s %15s ",StudentName,StudentAge);
    }
}
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
0

In Your for loop, you are printing an object.

What I tried is:

for (Student s : studentList) {
    writer.write(s.StudentName+"|"+s.StudentAge);
}

I used object s to call the variables and I got everything in right format, not gibberish the issue that you are facing.

Output for me is:

Gondi|68
George|52
Aaron|25
Gavin|36
joe|42
paul
  • 4,333
  • 16
  • 71
  • 144
0

The thing is, ArrayLists implement Serializable by default.

So if you got an ArrayList you can output to file just like this :

            ArrayList<Student> studentList = new ArrayList<Student>(); 
            studentList.add(new Student("Gondi", 68));
            studentList.add(new Student("George", 52));
            studentList.add(new Student("Aaron", 25));

            FileOutputStream fileOut = new FileOutputStream("/tmp/student.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(studentList);

You can read the whole array from the file just like this :

            FileInputStream fileIn = new FileInputStream("/tmp/student.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            ArrayList<Student> arrFromFile = (ArrayList<Artist>) in.readObject();
            For (Student s : arrFromFile) {
                  s.toString(); 
            }
Spoting
  • 23
  • 5