Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem in writing hashtable to file system.

Reply
Thread Tools

Problem in writing hashtable to file system.

 
 
M
Guest
Posts: n/a
 
      07-13-2004
Hi All,

I have problem in writing a hashtable to the file system
and hope you can give me some hints. The problem is like
this:

My hashtable consists of a key of type "String" and the
value is a object of a class WorkNature:
class WorkNature {
String responsible;
Vector duties;
}

It seems that I can write data to the hashtable (myHashTable)
but when I try to put it to the file system, there's some
problem. The code I used was:

void writeSysFile() {
final String PHY_FILE = "d:/Work_Nature";
try {
ObjectOutputStream os =
new ObjectOutputStream(new FileOutputStream(PHY_FILE));
os.writeObject(myHashTable);
os.close();
} catch (Exception e) {
System.out.println("writeSysFile: " + e.getLocalizedMessage());
}
}

Error "writeFile: myJavaProgram$WorkNature" was displayed !

Any ideas?
Thanks.





 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      07-13-2004
On Wed, 14 Jul 2004 02:01:41 +0800, M wrote:

> } catch (Exception e) {
> System.out.println("writeSysFile: " + e.getLocalizedMessage());
> }
> }
>
> Error "writeFile: myJavaProgram$WorkNature" was displayed !


....
} catch (Exception e) {
e.printStackTrace();
}
.....

produces the kind of much more detailed output
that you can see an example of, here..
<http://www.physci.org/codes/javafaq.jsp#exact>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
 
M
Guest
Posts: n/a
 
      07-13-2004

"Andrew Thompson" <(E-Mail Removed)> wrote in message
news:q9itbtc0ld02$(E-Mail Removed)...
> On Wed, 14 Jul 2004 02:01:41 +0800, M wrote:
>
> > } catch (Exception e) {
> > System.out.println("writeSysFile: " + e.getLocalizedMessage());
> > }
> > }
> >
> > Error "writeFile: myJavaProgram$WorkNature" was displayed !

>
> ...
> } catch (Exception e) {
> e.printStackTrace();
> }
> ....
>
> produces the kind of much more detailed output
> that you can see an example of, here..
> <http://www.physci.org/codes/javafaq.jsp#exact>
>
> --
> Andrew Thompson
> http://www.PhySci.org/ Open-source software suite
> http://www.PhySci.org/codes/ Web & IT Help
> http://www.1point1C.org/ Science & Technology


Well, after changing the catch to e.printStackTrace(),
my program displayed something like:

java.io.NotSerializableException: myJavaProgram$WorkNature
at
java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1054)
at
java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
at java.util.Hashtable.writeObject(Hashtable.java:806 )
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39
)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl
..java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
java.io.ObjectStreamClass.invokeWriteObject(Object StreamClass.java:809)
at
java.io.ObjectOutputStream.writeSerialData(ObjectO utputStream.java:1296)
at
java.io.ObjectOutputStream.writeOrdinaryObject(Obj ectOutputStream.java:1247)
at
java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1052)
at
java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
at myJavaProgram.writeSysFile(myJavaProgram.java:61)

Woo, that's totally too technically for me!
Any clue? Thanks.




 
Reply With Quote
 
Lukas Weibel
Guest
Posts: n/a
 
      07-13-2004
Of course the whole content of a hashtable needs to be serializable.
Therefore your WorkNature implementing Serializable:

class WorkNature implements Serializable {
String responsible;
Vector duties;
}

M wrote:

> Hi All,
>
> I have problem in writing a hashtable to the file system
> and hope you can give me some hints. The problem is like
> this:
>
> My hashtable consists of a key of type "String" and the
> value is a object of a class WorkNature:
> class WorkNature {
> String responsible;
> Vector duties;
> }
>
> It seems that I can write data to the hashtable (myHashTable)
> but when I try to put it to the file system, there's some
> problem. The code I used was:
>
> void writeSysFile() {
> final String PHY_FILE = "d:/Work_Nature";
> try {
> ObjectOutputStream os =
> new ObjectOutputStream(new FileOutputStream(PHY_FILE));
> os.writeObject(myHashTable);
> os.close();
> } catch (Exception e) {
> System.out.println("writeSysFile: " + e.getLocalizedMessage());
> }
> }
>
> Error "writeFile: myJavaProgram$WorkNature" was displayed !
>
> Any ideas?
> Thanks.
>
>
>
>
>

 
Reply With Quote
 
M
Guest
Posts: n/a
 
      07-13-2004

"Lukas Weibel" <(E-Mail Removed)> wrote in message
news:40f42b56$(E-Mail Removed)...
> Of course the whole content of a hashtable needs to be serializable.
> Therefore your WorkNature implementing Serializable:
>
> class WorkNature implements Serializable {
> String responsible;
> Vector duties;
> }
>
> M wrote:
>
> > Hi All,
> >
> > I have problem in writing a hashtable to the file system
> > and hope you can give me some hints. The problem is like
> > this:
> >
> > My hashtable consists of a key of type "String" and the
> > value is a object of a class WorkNature:
> > class WorkNature {
> > String responsible;
> > Vector duties;
> > }
> >
> > It seems that I can write data to the hashtable (myHashTable)
> > but when I try to put it to the file system, there's some
> > problem. The code I used was:
> >
> > void writeSysFile() {
> > final String PHY_FILE = "d:/Work_Nature";
> > try {
> > ObjectOutputStream os =
> > new ObjectOutputStream(new FileOutputStream(PHY_FILE));
> > os.writeObject(myHashTable);
> > os.close();
> > } catch (Exception e) {
> > System.out.println("writeSysFile: " + e.getLocalizedMessage());
> > }
> > }
> >
> > Error "writeFile: myJavaProgram$WorkNature" was displayed !
> >
> > Any ideas?
> > Thanks.
> >
> >
> >
> >
> >


Thanks. But I'm new to Java and never heard about Serializable...
The Java API doc is too complicated to me.. Do you mind
give me some brief explanation?




 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      07-13-2004
M wrote:
> Thanks. But I'm new to Java and never heard about Serializable...
> The Java API doc is too complicated to me.. Do you mind
> give me some brief explanation?


http://java.sun.com/docs/books/tutor...alization.html

I'm trying to understand how you managed to write code specifically for
the purpose of doing serialization, and yet never managed to hear about
Serializable. Nevertheless, that URL should help fill in the blanks.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      07-13-2004
M wrote:

> Well, after changing the catch to e.printStackTrace(),
> my program displayed something like:
>
> java.io.NotSerializableException: myJavaProgram$WorkNature
> at
> java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1054)
> at
> java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
> at java.util.Hashtable.writeObject(Hashtable.java:806 )
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39
> )
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl
> .java:25)
> at java.lang.reflect.Method.invoke(Method.java:324)
> at
> java.io.ObjectStreamClass.invokeWriteObject(Object StreamClass.java:809)
> at
> java.io.ObjectOutputStream.writeSerialData(ObjectO utputStream.java:1296)
> at
> java.io.ObjectOutputStream.writeOrdinaryObject(Obj ectOutputStream.java:1247)
> at
> java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1052)
> at
> java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
> at myJavaProgram.writeSysFile(myJavaProgram.java:61)
>
> Woo, that's totally too technically for me!


Then you'd best give up programming. It's one thing to not understand
what you see there, and indeed I wouldn't necessarily expect a novice to
understand it immediately, but if it's really "too technical" then you
are studying the wrong subject. In case it might not really be over
your head, here's how to read that:

An exception of type java.io.NotSerializableException and specifying a
detail message of "myJavaProgram$WorkNature" was thrown by the method
writeObject0 of class java.io.ObjectOutputStream, specifically from the
code at line 1054 in the class' source. That method was invoked by the
writeObject method of java.io.ObjectOutputStream, at line 278 in the
class' source. THAT method was invoked [...]. Ultimately you get down
to the place in your own code that was involved: the writeSysFile method
of class myJavaProgram, at line 61 in your source.

> Any clue? Thanks.


This exception is thrown when an object is required to be Serializable,
but isn't. The detail message specifies the name of the class of the
not Serializable object: myJavaProgram$WorkNature, an inner class of
myJavaProgram with unqualified name "WorkNature". That class can be
made Serializable simply by declaring that it implements the
java.io.Serializable interface.

Go ahead and try that to see what happens. My guess is that the file
you write will not be what you want, in that it will not be
human-readable. If you want to produce a human-readable listing of the
contents of your Hashtable then you will need to control the formatting
yourself in one way or another. For you, that probably means extracting
the Hashtable entries one by one, and writing an appropriate string to
the output file for each.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Lukas Weibel
Guest
Posts: n/a
 
      07-13-2004
Not at all. We all started small once.

With Serializable you mark (Seriablizable is a so call marker interface,
it does not expose any methods) a class as serializable. This has
something to do with security. Consider a bad person can serialize your
objects and read their content in the file system.

You can find further information at
http://java.sun.com/j2se/1.4.2/docs/...ion/index.html.

Regards
Lukas


M wrote:

> "Lukas Weibel" <(E-Mail Removed)> wrote in message
> news:40f42b56$(E-Mail Removed)...
>
>>Of course the whole content of a hashtable needs to be serializable.
>>Therefore your WorkNature implementing Serializable:
>>
>>class WorkNature implements Serializable {
>>String responsible;
>>Vector duties;
>>}
>>
>>M wrote:
>>
>>
>>>Hi All,
>>>
>>>I have problem in writing a hashtable to the file system
>>>and hope you can give me some hints. The problem is like
>>>this:
>>>
>>>My hashtable consists of a key of type "String" and the
>>>value is a object of a class WorkNature:
>>> class WorkNature {
>>> String responsible;
>>> Vector duties;
>>> }
>>>
>>>It seems that I can write data to the hashtable (myHashTable)
>>>but when I try to put it to the file system, there's some
>>>problem. The code I used was:
>>>
>>> void writeSysFile() {
>>> final String PHY_FILE = "d:/Work_Nature";
>>> try {
>>> ObjectOutputStream os =
>>> new ObjectOutputStream(new FileOutputStream(PHY_FILE));
>>> os.writeObject(myHashTable);
>>> os.close();
>>> } catch (Exception e) {
>>> System.out.println("writeSysFile: " + e.getLocalizedMessage());
>>> }
>>> }
>>>
>>>Error "writeFile: myJavaProgram$WorkNature" was displayed !
>>>
>>>Any ideas?
>>>Thanks.
>>>
>>>
>>>
>>>
>>>

>
>
> Thanks. But I'm new to Java and never heard about Serializable...
> The Java API doc is too complicated to me.. Do you mind
> give me some brief explanation?
>
>
>
>

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      07-13-2004
M wrote:
> Woo, that's totally too technically for me!
> Any clue? Thanks.


Interpreting exception stack trace output is a fundamental skill that
will be important to you in doing any software development in Java.
There are three parts you care about:

1. The exception class
2. The detail message
3. The stack itself

In your case:

> java.io.NotSerializableException: myJavaProgram$WorkNature


The exception is java.io.NotSerializableException. You can look that up
in the API documentation, and read all about this exception.

The detail message is "myJavaProgram$WorkNature". The exact meaning of
the detail message depends on which exception class you're dealing with.
In the case of NotSerializableException, the detail message tells you
the class name of the object that isn't serializable. It's your
WorkNature class (which is apparently a nested class inside of a class
called myJavaProgram; as an aside, myJavaProgram should really have been
called MyJavaProgram to avoid confusion).

The rest is the stack trace, which tells you where the exception
occurred. You can generally skip lines until you see something that's
your own code. So here we go:

> at
> java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1054)
> at
> java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
> at java.util.Hashtable.writeObject(Hashtable.java:806 )
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39
> )
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl
> .java:25)
> at java.lang.reflect.Method.invoke(Method.java:324)
> at
> java.io.ObjectStreamClass.invokeWriteObject(Object StreamClass.java:809)
> at
> java.io.ObjectOutputStream.writeSerialData(ObjectO utputStream.java:1296)
> at
> java.io.ObjectOutputStream.writeOrdinaryObject(Obj ectOutputStream.java:1247)
> at
> java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1052)
> at
> java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:27
> at myJavaProgram.writeSysFile(myJavaProgram.java:61)


And there you go. So to find the code that caused this exception, look
at line number 61 of myJavaProgram.java.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
M
Guest
Posts: n/a
 
      07-13-2004
"Lukas Weibel" <(E-Mail Removed)> wrote in message
news:40f43277$(E-Mail Removed)...
> Not at all. We all started small once.
>
> With Serializable you mark (Seriablizable is a so call marker interface,
> it does not expose any methods) a class as serializable. This has
> something to do with security. Consider a bad person can serialize your
> objects and read their content in the file system.
>
> You can find further information at
> http://java.sun.com/j2se/1.4.2/docs/...ion/index.html.
>
> Regards
> Lukas
>
>
> M wrote:
>
> > "Lukas Weibel" <(E-Mail Removed)> wrote in message
> > news:40f42b56$(E-Mail Removed)...
> >
> >>Of course the whole content of a hashtable needs to be serializable.
> >>Therefore your WorkNature implementing Serializable:
> >>
> >>class WorkNature implements Serializable {
> >>String responsible;
> >>Vector duties;
> >>}
> >>
> >>M wrote:
> >>
> >>
> >>>Hi All,
> >>>
> >>>I have problem in writing a hashtable to the file system
> >>>and hope you can give me some hints. The problem is like
> >>>this:
> >>>
> >>>My hashtable consists of a key of type "String" and the
> >>>value is a object of a class WorkNature:
> >>> class WorkNature {
> >>> String responsible;
> >>> Vector duties;
> >>> }
> >>>
> >>>It seems that I can write data to the hashtable (myHashTable)
> >>>but when I try to put it to the file system, there's some
> >>>problem. The code I used was:
> >>>
> >>> void writeSysFile() {
> >>> final String PHY_FILE = "d:/Work_Nature";
> >>> try {
> >>> ObjectOutputStream os =
> >>> new ObjectOutputStream(new FileOutputStream(PHY_FILE));
> >>> os.writeObject(myHashTable);
> >>> os.close();
> >>> } catch (Exception e) {
> >>> System.out.println("writeSysFile: " +

e.getLocalizedMessage());
> >>> }
> >>> }
> >>>
> >>>Error "writeFile: myJavaProgram$WorkNature" was displayed !
> >>>
> >>>Any ideas?
> >>>Thanks.
> >>>
> >>>
> >>>
> >>>
> >>>

> >
> >
> > Thanks. But I'm new to Java and never heard about Serializable...
> > The Java API doc is too complicated to me.. Do you mind
> > give me some brief explanation?
> >
> >
> >
> >


Thanks.
After I put the "... implements Serializable" stuff in my class,
it still give me the same error...

Anything I might done wrong?
Thx.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: hashtable or map? (map inserts not behaving as I expect - and I cant find a decent simple example for hashtable) Kai-Uwe Bux C++ 1 12-21-2008 09:25 PM
Hashtable problem =?Utf-8?B?QUQ=?= ASP .Net 6 02-25-2005 10:49 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Problem with hashTable Guillermo Perl 1 03-04-2004 12:43 PM
hashtable problem Ralf Bednarz Java 5 07-17-2003 09:36 PM



Advertisments