Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

it keeps rewrite data in text file

 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to write data into text file using textfield in small application.

and everytime i write, it keeps rewrite the data in text file.

first time i write : hello how are you

2nd time i write : my name is nick

----
why "my name is nick" will replace "hello how are you" in the text file when i open it?

which method to enter next new line in text file? or the class i used was wrong one?

here is my code


String text= textfield.getText();

BufferedWriter bw= new BufferedWriter(new FileWriter("a.txt");

bw.write(text);
bw.close()


[ August 25, 2006: Message edited by: Nicky Eng ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You found FileWriter and got through the tricky bits of building up buffered writer on it. Go back in the JavaDoc and look at all the constructors on FileWriter. See if one sparks a flame for you.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a constructor takes String and boolean. i know.

i want not to append but to enter new line in the text file everytime i enter new data into the file...
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm missing something there because what you want and what you have sound the same to me.

Show me what you'd like the file to look like after the first write, and again after the second write. How is what you're actually getting different from that?
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
text.txt
-------------------------------------------------------------
1st line in text file. first data comes in. then i close input application.
2nd line here. open input application. 2nd time data saved in this line.
3nd line here. keep doing that, and data keep saving in the new line everytime data in.


--------------------------------------------------------------

that's what i want.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about adding

to your code after writing a line but before closing the file.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicky Eng:

i want not to append but to enter new line in the text file everytime i enter new data into the file...



So you *want* to *append* a *new line*, or so it seems to me?
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Walin:
How about adding

to your code after writing a line but before closing the file.





not working.
bw.write("\n");
bw.write("\r");

results "2 small standing retangcle right after the data".
text file(NotePad) can accept "\n" or "\r" to enter to next line automatically?!?!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use

bw.newLine();

And don't use Notepad to look at text files - it's a rather crippled program. Use Textpad or something.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sovled.
thank you seniors.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add a new line to a text file do not use '\n' because different OS uses different character combinations for new line. So always use the newLine() method in BufferedWriter class to write a new line.
 
Hey! You're stepping on my hand! Help me tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic