Link to home
Start Free TrialLog in
Avatar of calicolon
calicolon

asked on

I wrote an Student[ ] array into a file as an Object, how can I read the file back into an Array

int capacity = 2;
int open = capacity;
theClass = new Student[capacity];


    for ( int i = 0 ; i < capacity ; i++ ) {

     
     theClass[i] = makeRoster( i + 1 );
     //System.out.println(theClass[i]);
     open --;
    } // end FOR(capacity)
 
 SelectionSort();
 File toFile = new File("Roster.txt");
ObjectOutputStream output = new ObjectOutputStream(
                    new FileOutputStream(toFile));




    for ( int i = 0 ; i < capacity ; i++ )
        output.writeObject( (i + 1) + "." +  theClass[i] +"\n" );
     output.close();

I have been trying to read it with the folowing code but it only gives me one line out of the file.
int canti;
File fromFile = new File("Roster.txt");
if ((fromFile.exists()) && (fromFile.canRead())){

Object newClass[] = new Object[(int)fromFile.length()];

FileInputStream fis = new FileInputStream(fromFile);
ObjectInputStream ois = new ObjectInputStream(fis);
canti = ois.read(newClass);
ois.close();
Object []std = (Student)ois.readObject();
ois.close();
System.out.print(std);
return;

this project is driving me crazy, as you can notice I am not a Java expert and been on this project for our and nothing had change. I still stock in it.
For al your help and time I THANK YOU ALL in advance.
Calicolon.
Avatar of VGR
VGR

is it normal for you to have two "ois.close();" statements ?
Avatar of calicolon

ASKER

I took the ois.close(); that was repeated and still giving me only one String back

int capacity = 2;
int open = capacity;
theClass = new Student[capacity];


   for ( int i = 0 ; i < capacity ; i++ ) {

   
    theClass[i] = makeRoster( i + 1 );
    //System.out.println(theClass[i]);
    open --;
   } // end FOR(capacity)

SelectionSort();
File toFile = new File("Roster.txt");
ObjectOutputStream output = new ObjectOutputStream(
                   new FileOutputStream(toFile));




   for ( int i = 0 ; i < capacity ; i++ )
       output.writeObject( (i + 1) + "." +  theClass[i] +"\n" );
    output.close();

I have been trying to read it with the folowing code but it only gives me one line out of the file.
int canti;
File fromFile = new File("Roster.txt");
if ((fromFile.exists()) && (fromFile.canRead())){

Object newClass[] = new Object[(int)fromFile.length()];

FileInputStream fis = new FileInputStream(fromFile);
ObjectInputStream ois = new ObjectInputStream(fis);
canti = ois.read(newClass);
Object []std = (Student)ois.readObject();
ois.close();
System.out.print(std);
return;
ASKER CERTIFIED SOLUTION
Avatar of igor_sk
igor_sk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
calicolon:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.