Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

arraylist  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi iam writing an arraylist to a text file. It works fine but some unwanted characters and symbols also get printed along with it. I tried with List.clear() but doesnt work. can you please suggest a method to do that?
This is what i get :
¬í psr java.util.ArrayListxÒ™Ça I sizexp w t CRN:34565t
CR NUMBER:N/At SOW NAME:SWEXCELt "PARTNER SERVICE MANAGER:joe mathewt LBG SERVICE MANAGER:lee stevet TYPE OF CONTRACT:SOWt VALUE OF CONTRACT:34567t REMARKS:goodt WORK STATUS:Livet APPROVAL:Fully signed offt PLATFORM:ADM-WPMt MODEL:T&Mt START DATE:15-08-2012t END DATE:25-08-2012t CUMULATIVE VALUE:5645x

Also how to print each item in the arraylist in the new line? I tried arrayList.add("CRN"+":"+obj1+"\n"); doesnt work either.

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When writing text files, better use a Writer (FileWriter) instead of FileOutputStream. And write each element of the list in a for loop, then you can simply add a newline in each iteration.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harini,

Welcome to CodeRanch!

Firstly, why are you using ArrayList? If you are generating output which is to be dumped in a file, it makes much sense to use StringBuilder etc. instead of ArrayList.

Secondly, if you are writing ArrayList object itself by using serialization mechanism (writeObject method), then it writes the actual object to file, which might not be readable by simply opening the file (i.e. the ArrayList is not written in text format). If you want to read it, you'll need to use readObject method.

If you simply want to write the text, then no need to go for serialization.

I hope this helps.
 
Harini Kumaresan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried as you said. doest work either the text file is empty.

Can you correct this piece of code, iam going wrong somewhere. cant figure it out.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try closing PrintWriter object:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!