vector - multiple entries

Hello.

I'm writing data to a file but i can't figure out how to add multiple entries. Every time i make a new entry the old data is wiped out.

Write vector example:

Vector vector =new Vector();

vector.add("element 1");

vector.add("element 2");

try{

FileOutputStream fout =new FileOutputStream("data.dat");

ObjectOutputStream oos =new ObjectOutputStream(fout);

oos.writeObject(vector);

oos.close();

}catch (Exception e){

e.printStackTrace();

}

Read vector:

Vector vector =new Vector();

try{

FileInputStream fin =new FileInputStream("data.dat");

ObjectInputStream ois =new ObjectInputStream(fin);

vector = (Vector) ois.readObject();

ois.close();

}catch (Exception e){

e.printStackTrace();

}

[1491 byte] By [johnp11a] at [2007-11-26 18:13:37]
# 1
i'm not english wirte very well.when i executed the source file , i was succedyou can try agian..!!ps . file path --> absolute file path
hydorgena at 2007-7-9 5:46:46 > top of Java-index,Core,Core APIs...
# 2

FileOutputStream fout = new FileOutputStream("data.dat");

This creates a new file every time.

Did you mean to append to the file? In which case that won't work for another reason ... You would have to read the file in as a Vector as per your code, add to the vector, then write it out again to a new file as above.

ejpa at 2007-7-9 5:46:46 > top of Java-index,Core,Core APIs...
# 3
I realize my sample code compiles, but thats not my problem. And yes, I suppose I'm trying to append to the file. Anyways, I'll try converting it into an array with the toArray() method.
johnp11a at 2007-7-9 5:46:46 > top of Java-index,Core,Core APIs...
# 4
Why?
ejpa at 2007-7-9 5:46:46 > top of Java-index,Core,Core APIs...
# 5

I've since found out that the FileOutputStream contains a boolean for appending files.

FileOutputStream("data.dat",true)

I can see that it appends to the file but it only extracts only one row, the first row, and ignores the rest. I tried using the iterator but it does not seem to work so far..

johnp11a at 2007-7-9 5:46:46 > top of Java-index,Core,Core APIs...
# 6
I told you it would't work in reply #2. You can't append ObjectOutputStreams. I also already told you what you need to do instead.
ejpa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 7
I see. Could you point me to an example?
johnp11a at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 8
Hi you would use RandomAccessFile class this is read and write, append is easy, and object format get back vector format,
SASIJAVAa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 9
Obviously you haven't tried it.Append is not 'easy', it is impossible with ObjectOutputStreams except via a horrible kludge.And you can't use RandomAccessFiles directly with ObjectOutputStreams.
ejpa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 10
Else you need to unbox the elements from teh collection and use a loop and append to file using the RandomAccessFile.
AbiSSa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 11

I'd go like this. Read the Vector from a FileInputStream and ObjectInputStream (on a FileNotFoundException only, create a new Vector). Add the new elements to the Vector. Write the Vector back using a FileOutputStream and ObjectOutputStream, thus overwriting the old Vector, as you've seen already. Is there more to it than this?

Message was edited by:

OleVV

OleVVa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 12

Got it. My order of operations was wrong.

Thanks.

WRITE

try {

FileOutputStream fout = new FileOutputStream("vector.dat");

ObjectOutputStream oos = new ObjectOutputStream(fout);

vector.add("element1");

oos.writeObject(vector);

oos.close();

} catch (Exception e) {

e.printStackTrace();

}

READ

try {

FileInputStream fin = new FileInputStream("vector.dat");

ObjectInputStream ois = new ObjectInputStream(fin);

vector = (Vector) ois.readObject();

ois.close();

} catch (Exception e) {

e.printStackTrace();

}

Message was edited by:

johnp11

johnp11a at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...
# 13
> use a loop and append to file using the RandomAccessFile.This makes no more sense the second time it was suggested than it did the first time. See replies 2, 6, and 9.
ejpa at 2007-7-9 5:46:47 > top of Java-index,Core,Core APIs...