aspose file tools*
The moose likes Beginning Java and the fly likes how to append in Java from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to append in Java from a file" Watch "how to append in Java from a file" New topic
Author

how to append in Java from a file

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Post your changed code for further assistance.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 23, 2012
Posts: 199
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

Joined: Jan 23, 2012
Posts: 199
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 ?
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
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....


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya. thank's a lot. the problem solved..



Getting the output Thank's to all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to append in Java from a file