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(theCl ass[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.lengt h()];
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.
int open = capacity;
theClass = new Student[capacity];
for ( int i = 0 ; i < capacity ; i++ ) {
theClass[i] = makeRoster( i + 1 );
//System.out.println(theCl
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.lengt
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.
is it normal for you to have two "ois.close();" statements ?
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(theCl ass[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.lengt h()];
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;
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(theCl
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.lengt
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.