• 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

how to append in Java from a file

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have n input file. I want the words in that to be appended.

help s1
start s2
solved s3


i would like to append the words in this like
|help
|help|start
|help|start|solved.


the code i have used for append



Problem i am facing. the append is not done after the word in the next line.
the output is like this:
|help
|start
|solved


I may not be clear in the explaining.But if you see the output you might be clear.
How to proceed this?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a new StringBuffer in a loop will obviously erase all previous content. (by the way, you may prefer using StringBuilder).
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:Creating a new StringBuffer in a loop will obviously erase all previous content. (by the way, you may prefer using StringBuilder).



ya. i have changed.But could not find any changes in the output.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your changed code for further assistance.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in case, when I said that you may prefer using StringBuilder, I didn't mean that this would fix your problem. Creating a new StringBuilder in a loop will still erase all previous content.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Post your changed code for further assistance.



I have changed only one line in the code.

is changed to

I am confused how to proceed. Guide me with the steps
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:Just in case, when I said that you may prefer using StringBuilder, I didn't mean that this would fix your problem. Creating a new StringBuilder in a loop will still erase all previous content.



ya i have changed that. thank you. Can you tell me with little more explanation, where to use StringBuffer and StringBulider ?
 
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).
So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.

As per the issue, if you had instantiated StringBuffer onject before while loop, you code would have generated the desired output


nir
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put your all words in one ArrayList.
Then Iterate that array list one by one.

The logic is:

First time append the one word into the StringBuffer or StringBuilder (Whatever you use)
Second time append the two words
Third time append the three words and so on....
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya. thank's a lot. the problem solved..



Getting the output Thank's to all
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic