aspose file tools*
The moose likes Beginning Java and the fly likes how to store an array or a list in a file in memory ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to store an array or a list in a file in memory ?" Watch "how to store an array or a list in a file in memory ?" New topic
Author

how to store an array or a list in a file in memory ?

pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
hi frnz,
i am making a program to save a list of messages and retrieve , edit modify these messages.
wen i stored the array in a file in memory nothing got stored in the file?
can anybody help?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11327
    
  16

what do you mean by you "stored the array in a file in memory"? I don't understand...

Can you elaborate, and maybe post a code sample? (keep the code as simple as possible but still demonstrate you point...)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Welcome to JavaRanch

Please avoid abbreviations like frnz; a lot of Ranchers who didn't grow up with English find them difficult to understand. Read this FAQ, please.
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
well what i mean by storing an array is that i hav to make a list of messages just like an inbox or outbox in phone. i hav used an arraylist to implement the list.
i want to store this list to the disc(that is in some file) and retrieve it too.
how to store this arraylist to a file??





the list is not stored in the file.how can i store an arraylist to the disc n retrieve it??

[edit]Add code tags. CR[/edit]
[ July 11, 2008: Message edited by: Campbell Ritchie ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by pdpta chopra:


the list is not stored in the file.how can i store an arraylist to the disc n retrieve it??


Well, what you have done seems to be right. Serialization is the rescue for it!

what gets written is a list and not a primitive whose values would be visible directly. If you want to see the contents of the list, you have to get it from the file (Deserialization -- reverse process) and then iterate it and obtain/print it.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Please use code tags (button below the message box); I have added them so you can see how much easier your code is to read. And the word is "and" not "n", please.

You can serialise the List, since most Lists implement Serializable. Or you can iterate through the List (probably best with a for-each loop) and write each line into your text file.
Actually for a plain text file where you are not appending, you will probably find java.util.Formatter and java.util.Scanner easier to use than Streams.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Originally posted by Raghavan Muthu:
What gets written is a list and not a primitive whose values would be visible directly.
Actually, if you go through a serialised file with a text editor, you can read quite a lot, all the Strings, for example.

Try it
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
well thanks for the hint to the above problem
and the next thing i do is "READ Faqs" so as to write a question properly
thanks
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
Isnt an arraylist type of list an object of class ArrayList??

when i write
out.writeObject(outbox);
where (ArrayList<NewMessage> outbox =new ArrayList<NewMessage>()

the compiler doesnt complie this code.
and i m not able to store outbox to a file
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Careful with speLlings please; always write ArrayList, not arraylist.

I can't see what's wrong with the last bit of code you quoted; please give fuller details of the compiler error message.
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
this is the code i have written
but it doesnt compile





all it gives is error writing to file

but if i write

outoboutbox.writeObject(name);
(name being a String)
then the output is

a message serializd into outbox
take a look

[edit]Change bold tags to code tags. CR[/edit]
[ July 11, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Sorry for delay; I have been out.

You haven't got a compiler error at all; what you have is a runtime error (exception). Put something informative inside your catch block; even e.printStackTrace(); is a lot more useful than what you have got because it tells you where the Exception occurred.

BTW: Always try to avoid catching or throwing or declaring Exception. Always use Exceptions down the inheritance tree if at all possible.

If it's a java.io.NotSerializableException then you need to check that your NewMessage class implements Serializable. That might be the cause of the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to store an array or a list in a file in memory ?