Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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.
0
calicolon
Asked:
calicolon
1 Solution
 
VGRCommented:
is it normal for you to have two "ois.close();" statements ?
0
 
calicolonAuthor Commented:
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;
0
 
igor_skCommented:
>>> output.writeObject( (i + 1) + "." +  theClass[i] +"\n" );

You should pass an object, which you want to write, to writeObject() method. Your writing loop will be as following:
for ( int i = 0 ; i < capacity ; i++ )
    output.writeObject(theClass[i]);

When you read these objects from the input stream, you read them one by one:
Student st = (Student)ois.readObject();






0
 
CleanupPingCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now