Re: [Newbie] Reading & Writing Objects To File

From: Michael Green (mikegreonline_at_microsoft.com)
Date: 03/10/04


Date: Wed, 10 Mar 2004 18:49:28 GMT

JJ,

Without seeing your code it is difficult to say what is going wrong. It
sounds like the data is not getting persisted correctly. Have you looked at
the code that Lars posted? He gave an excellent example of serializing
objects. If you can post a sample that reproduces the problem, I'd be happy
to take a look at it for you.

Thanks,

Michael Green
Microsoft Developer Support

--------------------
| From: jjyooi@yahoo.com (Loopy)
| Newsgroups: microsoft.public.dotnet.vjsharp
| Subject: Re: [Newbie] Reading & Writing Objects To File
| Date: 7 Mar 2004 08:34:24 -0800
| Organization: http://groups.google.com
| Lines: 134
| Message-ID: <f3e35b43.0403070834.6602f67f@posting.google.com>
| References: <2bhh30drq4rqsgmn27l4bh6npkju8uukb3@4ax.com>
<ua5skXY#DHA.3436@tk2msftngp13.phx.gbl>
<#nCH8rj#DHA.1428@TK2MSFTNGP12.phx.gbl>
<drik30lcqo3iv65ch3g00d93vd1o7iogr5@4ax.com>
<un#jWWk#DHA.2524@TK2MSFTNGP11.phx.gbl>
<#eIz9vq#DHA.2636@TK2MSFTNGP09.phx.gbl>
| NNTP-Posting-Host: 80.189.121.145
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1078677265 16844 127.0.0.1 (7 Mar 2004
16:34:25 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: Sun, 7 Mar 2004 16:34:25 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!border2.nntp.ash.giganews.c
om!border1.nntp.ash.giganews.com!nntp.giganews.com!news.glorb.com!postnews1.
google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.vjsharp:5611
| X-Tomcat-NG: microsoft.public.dotnet.vjsharp
|
| Hi, sorry for the late response.
|
| I'm still having problems, and I'm still getting a
| java.io.StreamCorruptedException exception. How can I find out what is
| causing this so that I can rectify it? Could it have something to do
| with the method names in my superclasses....?
|
| Regards
|
| JJ
|
| "Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> wrote in message
news:<#eIz9vq#DHA.2636@TK2MSFTNGP09.phx.gbl>...
| > > [Your code] private final Vector objects = new Vector();
| > >
| > > This means you can't write anything in "objects". If you try to write
| > > anything here, you will get an exception. I suggest:
| >
| > No. The final keyword only means that the "objects" field cannot be
| > reassigned.
| > But you can store objects into it, clear it, etc.
| >
| > Also, "final" gives compile time diagnostics, not run-time diagnostics
| > (except if you try to reassign the field via Reflection).
| >
| > Bruno
| >
| > >
| > > [new] private Vector objects = new Vector();
| > >
| > >
| > >
| > > public static void main(String[] args)
| > > {
| > > new CardCreatorDump();
| > > try
| > > {
| > > ObjectInputStream is = new
| > > ObjectInputStream(new FileInputStream("CardInfo.dat"));
| > > Object o = is.readObject();
| > > System.out.println(o.getClass() + "/" +o.GetType());
| > > }
| > > catch (Exception e)
| > > {
| > > e.printStackTrace();
| > > }
| > > }
| > >
| > >
| > > I suggest moving everything inside the object.
| > > public static void main(String[] args)
| > > {
| > > new CardCreatorDump();
| > > }
| > >
| > >
| > >
| > >
| > >
| > > This code does not give me any exceptions.
| > >
| > > import java.util.*;
| > > import java.io.*;
| > >
| > > // Make sure your objects are serializable
| > > public class DarkPiercingLight implements Serializable
| > > {
| > > public System.String data;
| > > }
| > >
| > >
| > > public class CardCreatorDump
| > > {
| > > private Vector objects = new Vector();
| > >
| > > public CardCreatorDump()
| > > {
| > > this.dump();
| > > this.ReadFromDisk();
| > > }
| > >
| > > public void dump()
| > > {
| > > try
| > > {
| > > System.out.println("Adding \"Dark Piercing Light\" To Dump
| > > List.");
| > > objects.addElement(new DarkPiercingLight());
| > > //Write the vector to disk
| > > System.out.print("Writing To Disk... ");
| > > ObjectOutputStream os = new ObjectOutputStream(new
| > > FileOutputStream("CardInfo.dat"));
| > > os.writeObject(objects);
| > > os.flush();
| > > os.close();
| > > System.out.println("Done.");
| > > }
| > > catch (Exception e)
| > > {
| > > System.err.println("Error: " +e.getMessage());
| > > e.printStackTrace();
| > > }
| > > }
| > >
| > > public void ReadFromDisk()
| > > {
| > > try
| > > {
| > > ObjectInputStream is = new ObjectInputStream(new
| > > FileInputStream("CardInfo.dat"));
| > >
| > > // this.objects = (Vector)is.readObject();
| > > Object o = is.readObject();
| > > System.out.println(o.getClass() + "/" +o.GetType());
| > > }
| > > catch (Exception e)
| > > {
| > > e.printStackTrace();
| > > }
| > > }
| > >
| > > public static void main(String[] args)
| > > {
| > > new CardCreatorDump();
| > > }
| > > }
| > >
| > >
| > > --
| > > Regards,
| > > Lars-Inge Tønnessen
| > > http://emailme.larsinge.com
| > > http://www.larsinge.com
| > >
| > >
|



Relevant Pages

  • Re: crushing java
    ... your reaction just proved you to be sentimental and self blinded ... public static void Main(stringargs) ... }catch(Exception e) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: crushing java
    ... your reaction just proved you to be sentimental and self blinded ... public static void Main(stringargs) ... }catch(Exception e) ...
    (microsoft.public.dotnet.framework)
  • Re: problem...sorry my first time with java
    ... public static void main{ ... java cl_gm ... the String array passed to the main method as args[] will be empty, ... You may wish to familiarise yourself with the javadoc for the exception ...
    (comp.lang.java.help)
  • Re: I need to know if a java class import a package
    ... to test if they can iterate an order an array by themselves). ... public static void main(Stringargs) throws Exception { ...
    (comp.lang.java.programmer)
  • Re: alternative to my ClassLoader hack
    ... public static void main(String... ... mth.invokeargs); ... public static void main(String... ... trying super class... ...
    (comp.lang.java.programmer)