This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

how to append in Java from a file

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
ya. thank's a lot. the problem solved..



Getting the output Thank's to all
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!