• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

arraylist

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try closing PrintWriter object:
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic