1,105,286 Community Members

Arraylist Save and Load

Member Avatar
frankycool
Newbie Poster
15 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI i am using this syntax to store arraylist to inventory.dat file.When i store, it give a scattered output on the .dat file like this.....

¬í sr Carsguimainexecution.SaleinfoÕE£ñWfö L productsalest Ljava/util/ArrayList;xpsr java.util.ArrayListxÒ™Ça I sizexp w
sr Carsguimainexecution.SalesubZã¾ÓÅ xr Carsguimainexecution.SaleProductøié>‘2q I CarnumberD pricesoldI
productNumberD stockD unitsoldL Carbuyert Ljava/lang/String;L Carnameq ~ xp t Defaultq ~ sq ~ @I @@ @$ t dsdggsdrft fgsgsfsq ~ @@ @Å| @& t dfgdrht gyjgyjsq ~ @@ @Çp @( t dfsbt fddndfsq ~ @@ @È @$ t sdfsdft sdfsfsfsq ~ @$ @y @D t dsdgrght hdhdhsq ~ @$ @@ @Y t fbdfbt dfbdfbsq ~ @$ @Y @$ t defaultt defaultsq ~ t Defaultt Defaultx


Is the output an error or its correct,and if its correct how can i load this information back to an arraylist.

To Store,this is what i use excluding the try and catch:-

File f = new File( "C:\\data\\inventory.dat" );
FileOutputStream out = new FileOutputStream( f );
ObjectOutputStream objectOut = new ObjectOutputStream( out );
objectOut.writeObject( inv );

This is a sample of how my arraylist looks like;

products.add( new CarsSubProduct( "FordMustang",1,1000,10000 ) );

Please any advice will be really helpful.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Because you are saving the object as binary data not as text. You are serializing. You cannot open the file and view it. If you want to read it then use the opposite way.

FileInputStream
ObjectInputStream

If you want to save it as text then use BufferedWriter, but it needs much more work. With your way, you did it in 3-4 lines.

I hope that your class implements the java.io.Serializable interface.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: