Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Appending to an disk file

0 views
Skip to first unread message

Eric Nagler

unread,
Feb 24, 2003, 1:59:11 PM2/24/03
to
I don't understand why this program aborts after I append to the
existing file using an ObjectOutputStream. I know that the data is
being appended, so why can't I read it back in?

import java.io.*;
public class Main
{
public static void main(String args[]) throws Exception
{
// Stream an int
FileOutputStream fos = new FileOutputStream("diskfile");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(111);
oos.close();

// Append a second int
fos = new FileOutputStream("diskfile", true);
oos = new ObjectOutputStream(fos);
oos.writeInt(222);
oos.close();

// Read and display both ints
FileInputStream fis = new FileInputStream("diskfile");
ObjectInputStream ois = new ObjectInputStream(fis);
int x = ois.readInt();
System.out.println(x); // Displays OK
int y = ois.readInt(); // Blows up here
System.out.println(y);
ois.close();
}
}

TIA...

Eric

Lothar Kimmeringer

unread,
Feb 24, 2003, 7:30:37 PM2/24/03
to
Eric Nagler <ericn...@earthlink.net> schrieb:

>I don't understand why this program aborts after I append to the
>existing file using an ObjectOutputStream. I know that the data is
>being appended, so why can't I read it back in?

Maybe the error appearing might help.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: spam...@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!

JaJ

unread,
Feb 25, 2003, 8:35:36 AM2/25/03
to
Eric Nagler <ericn...@earthlink.net> wrote in message news:<4mqk5voklstltr59h...@4ax.com>...

Hard to say without the error message - however fos might not have

> // Read and display both ints
> FileInputStream fis = new FileInputStream("diskfile");
> ObjectInputStream ois = new ObjectInputStream(fis);
> int x = ois.readInt();
> System.out.println(x); // Displays OK
> int y = ois.readInt(); // Blows up here
> System.out.println(y);
> ois.close();

However try:-
BufferedReader br=new BufferedReader(ois);
String s = br.readLine();
while (s != null)
{
System.out.println(s); //yeah bad way of doing it I know.
s=br.readLine();
}

This will loop and read in all the lines - just to check that you can
do the reading in and printing of the objects.

But the error message would be real helpful.

JaJ

Steve Horsley

unread,
Feb 25, 2003, 1:06:53 PM2/25/03
to
Lothar Kimmeringer <news2...@kimmeringer.de> wrote in message news:<p8el5vomvognoc0nt...@4ax.com>...

> Eric Nagler <ericn...@earthlink.net> schrieb:
>
> >I don't understand why this program aborts after I append to the
> >existing file using an ObjectOutputStream. I know that the data is
> >being appended, so why can't I read it back in?
>
> Maybe the error appearing might help.
>
>
> Regards, Lothar

It certainly would. However, I guess it's probably due to the fact that
he doesn't reset the ObjectInputStream to match the start of a
new ObjectOutputStream's data.

Try calling ois.reset() between the two reads.

Steve

Eric Nagler

unread,
Feb 25, 2003, 6:30:52 PM2/25/03
to
On 25 Feb 2003 10:06:53 -0800, steve....@cwcom.cwplc.com (Steve
Horsley) wrote:

>Lothar Kimmeringer <news2...@kimmeringer.de> wrote in message news:<p8el5vomvognoc0nt...@4ax.com>...
>> Eric Nagler <ericn...@earthlink.net> schrieb:
>>
>> >I don't understand why this program aborts after I append to the
>> >existing file using an ObjectOutputStream. I know that the data is
>> >being appended, so why can't I read it back in?
>>
>> Maybe the error appearing might help.
>>
>>
>> Regards, Lothar
>
>It certainly would.

Oops....my mistake. I'm getting:

111
Exception in thread "main" java.io.StreamCorruptedException
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader
InputStream.java:2335)
at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectIn
am.java:2368)
at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInpu
.java:2440)
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectI
eam.java:2645)
at java.io.ObjectInputStream.readInt(ObjectInputStream.java:892)
at Main.main(Main.java:24)

> However, I guess it's probably due to the fact that
>he doesn't reset the ObjectInputStream to match the start of a
>new ObjectOutputStream's data.

Not sure what that means.

>Try calling ois.reset() between the two reads.

I added it, and now get:

111
Exception in thread "main" java.io.IOException: mark/reset not
supported
at java.io.InputStream.reset(InputStream.java:329)
at Main.main(Main.java:24)


Eric Nagler

unread,
Feb 25, 2003, 6:35:43 PM2/25/03
to

Sorry, doesn't compile. BufferedReader takes a Reader in the ctor, and
"ois" is not a Reader.

>But the error message would be real helpful.

Please see the other message in this thread. Thanks...

Eric

Steve Horsley

unread,
Feb 26, 2003, 10:50:51 AM2/26/03
to
Eric Nagler <ericn...@earthlink.net> wrote in message news:<ivun5vs87evfllc16...@4ax.com>...

> On 25 Feb 2003 10:06:53 -0800, steve....@cwcom.cwplc.com (Steve
> Horsley) wrote:
>
> >Lothar Kimmeringer <news2...@kimmeringer.de> wrote in message news:<p8el5vomvognoc0nt...@4ax.com>...

> > However, I guess it's probably due to the fact that
> >he doesn't reset the ObjectInputStream to match the start of a
> >new ObjectOutputStream's data.
>
> Not sure what that means.
>

When you create a new ObjectOutputStream, it writes a header block.
Since you append the file by creating another ObjectOutputStream,
I guess your file contains:
Header | Data | Header | Data

But you are ignoring the presence of the extra header, and trying
to read data there instead.

> >Try calling ois.reset() between the two reads.
>
> I added it, and now get:
>
> 111
> Exception in thread "main" java.io.IOException: mark/reset not
> supported
> at java.io.InputStream.reset(InputStream.java:329)
> at Main.main(Main.java:24)

Hmm. Then you probably need to create a new ObjectInputStream to
absorb the extra header.

If you don't intend to write Objects, you may be better off with
DataOutputStream which doesn't use headers.

Steve

Eric Nagler

unread,
Feb 26, 2003, 11:39:42 AM2/26/03
to
On 26 Feb 2003 07:50:51 -0800, steve....@cwcom.cwplc.com (Steve
Horsley) wrote:

>Eric Nagler <ericn...@earthlink.net> wrote in message news:<ivun5vs87evfllc16...@4ax.com>...
>> On 25 Feb 2003 10:06:53 -0800, steve....@cwcom.cwplc.com (Steve
>> Horsley) wrote:
>>
>> >Lothar Kimmeringer <news2...@kimmeringer.de> wrote in message news:<p8el5vomvognoc0nt...@4ax.com>...
>
>> > However, I guess it's probably due to the fact that
>> >he doesn't reset the ObjectInputStream to match the start of a
>> >new ObjectOutputStream's data.
>>
>> Not sure what that means.
>>
>
>When you create a new ObjectOutputStream, it writes a header block.
>Since you append the file by creating another ObjectOutputStream,
>I guess your file contains:
>Header | Data | Header | Data
>
>But you are ignoring the presence of the extra header, and trying
>to read data there instead.

If that's the case, it makes no sense to me. What's so special about
an ObjectOutoutStream that it has to create a "header" in the middle
of my data? I can append to a "normal" disk output file and not
encounter this problem.

>
>> >Try calling ois.reset() between the two reads.
>>
>> I added it, and now get:
>>
>> 111
>> Exception in thread "main" java.io.IOException: mark/reset not
>> supported
>> at java.io.InputStream.reset(InputStream.java:329)
>> at Main.main(Main.java:24)
>
>Hmm. Then you probably need to create a new ObjectInputStream to
>absorb the extra header.

Fine, if I could just figure out the code to "absorb" a header (or
whatever misc. junky data is in there).

>If you don't intend to write Objects, you may be better off with
>DataOutputStream which doesn't use headers.

Well, the whole idea is to write a Vector to disk, quit the program,
come back later and add more stuff to the Vector, and write it out to
disk again. I just figured I needed an ObjectOutputStream in order to
achieve persistence for the data. Am I missing something obvious?

Eric

JaJ

unread,
Feb 26, 2003, 7:32:35 PM2/26/03
to
Eric Nagler <ericn...@earthlink.net> wrote in message news:<0cvn5vsjm72i5h4jj...@4ax.com>...

> On 25 Feb 2003 05:35:36 -0800, jaj_de...@yahoo.co.uk (JaJ) wrote:
>
> >Eric Nagler <ericn...@earthlink.net> wrote in message news:<4mqk5voklstltr59h...@4ax.com>...
> >
> >Hard to say without the error message - however fos might not have
> >
> >> // Read and display both ints
> >> FileInputStream fis = new FileInputStream("diskfile");
> >> ObjectInputStream ois = new ObjectInputStream(fis);
> >> int x = ois.readInt();
> >> System.out.println(x); // Displays OK
> >> int y = ois.readInt(); // Blows up here
> >> System.out.println(y);
> >> ois.close();
> >
> >However try:-
> >BufferedReader br=new BufferedReader(ois);
> >String s = br.readLine();
> >while (s != null)
> >{
> > System.out.println(s); //yeah bad way of doing it I know.
> > s=br.readLine();
> >}
> >
> >This will loop and read in all the lines - just to check that you can
> >do the reading in and printing of the objects.
>
> Sorry, doesn't compile. BufferedReader takes a Reader in the ctor, and
> "ois" is not a Reader.

I meant (new InputStreamReader("fis"))

OK then:-

String line;
BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(filename)));
while ( (line = br.readLine())!=null)
{
//do something with line.
}
br.close();

I must admit I prefer Buffered Reader/writer. But thats just me.

> Please see the other message in this thread. Thanks...

NP.

JaJ

Eric Nagler

unread,
Feb 27, 2003, 6:02:03 PM2/27/03
to

But I still need the ObjectInputStream in there somewhere, as the
whole point is to reconstitute the objects. The program now reads:

import java.io.*;
public class Main
{
public static void main(String args[]) throws Exception
{
// Stream an int
FileOutputStream fos = new FileOutputStream("diskfile");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(111);
oos.close();

// Append a second int
fos = new FileOutputStream("diskfile", true);
oos = new ObjectOutputStream(fos);
oos.writeInt(222);
oos.close();

// Read and display....
BufferedReader br = new BufferedReader
(new InputStreamReader
(new ObjectInputStream
(new FileInputStream("diskfile"))));
String line;
while((line = br.readLine()) != null)
System.out.println(line);
br.close();
}
}

Not sure what's supposed to happen, but I now get:

Exception in thread "main" java.io.StreamCorruptedException
at

java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Object
InputStream.java:2335)
at
java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStre
am.java:2368)
at
java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream
.java:2527)
at java.io.ObjectInputStream.read(ObjectInputStream.java:786)
at
sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:404)
at
sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:442)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:179)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Main.main(Main.java:33)

Steve Horsley

unread,
Feb 28, 2003, 8:38:11 AM2/28/03
to
Eric Nagler <ericn...@earthlink.net> wrote in message news:<b6rp5vcklc2nek43o...@4ax.com>...

I think you're missing 2 things.

The API documentation clearly says that ObjectOutputStream writes
a header. I seem to remember it's in the constructor documentation.
You may find the exact header format there. I don't advise trying
to skip it manually - ObjectOutputStream and ObjectInputStream
are stateful things. Hiding the header and connecting your used
ObjectInputStream to a fresh OutputStream will probably go bad.
Maybe that's the reason for the header.

Writing a second Vector object at the end the file will not append
things to the original Vector. It will leave 2 distinct Vectors on
file (or 2 conflicting pictures of the same Vector). I suggest that
you load the Vector on startup, edit in memory as required (using
myVector.add(Object), and overwrite the old file before shutdown.
Don't do any appending at all.

Steve

Steve

Eric Nagler

unread,
Feb 28, 2003, 11:52:05 AM2/28/03
to
On 28 Feb 2003 05:38:11 -0800, steve....@cwcom.cwplc.com (Steve
Horsley) wrote:

Thanks for the feedback. I'll try implementing your suggestion.

Eric

0 new messages