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.

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.


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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.