?
Solved

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

Posted on 2003-03-19
5
Medium Priority
?
241 Views
Last Modified: 2010-04-17
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
Comment
Question by:calicolon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8170541
is it normal for you to have two "ois.close();" statements ?
0
 

Author Comment

by:calicolon
ID: 8170610
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
 
LVL 1

Accepted Solution

by:
igor_sk earned 200 total points
ID: 8171538
>>> 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
 

Expert Comment

by:CleanupPing
ID: 9446979
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question