Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

it keeps rewrite data in text file  RSS feed

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about adding

to your code after writing a line but before closing the file.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
sovled.
thank you seniors.
 
Sanjay Siva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!