Saving JTextAreas to a text file.

Hey,I currently have a JTextArea set up and I am trying to implement a save button, so that it saves the text contents back to a file of the users choice.Can anyone help,Cheers.
[205 byte] By [nickbob_sexypantsa] at [2007-11-26 16:06:23]
# 1
what have you come up with so far? I've answered some of your questions before, and I know you have the ability to do this already, if you only had the confidence to try
georgemca at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 2

else if(source == btnsave)

{

try

{

String text= ta.getText();

FileWriter fw = new FileWriter ("Nick.java");

BufferedWriter out = new BufferedWriter (fw);

PrintWriter outFile = new PrintWriter (bw);

for(int i=0; i<10; i++)

{

out.write(text);

}

out.close();

}

catch(IOException ioe1){}

}

This is what I have so far, I have messed for hours so I think I may have gone further and further away.

Cheers

nickbob_sexypantsa at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 3
and what doesn't work? is the code you posted being called, but not working, or not being called in the first place?btw, if you wrap your posted code in [code] tags, it makes it much easier for us to read!
georgemca at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 4

sorry, will do that in future.

It compiles, but in this case in the code so far I have specified the file, when I check the file however it is still the same as it was before.

I think it is not saving it out properly, because it does not overwrite what is already in the file.

Cheers

nickbob_sexypantsa at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 5

how are you checking if the file is being changed? using a text editor? do you close it down afterwards?

you're ignoring IOExceptions, which is A Bad Idea ™. put some code in that empty catch block, see what's happening. might be that your text editor is keeping the file open, so your code can't overwrite it. but you won't know that's happening because you're ignoring IOExceptions

georgemca at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 6

Another point: it easy to miss methods introduced by superclasses. Check out the methods of JTextComponent -- the superclass of JTextArea. You may find the its [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#read(java.io.Reader,%20java.lang.Object)]read()[/url] and [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#write(java.io.Writer)]write()[/url] methods darn handy.

DrLaszloJamfa at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 7

> Another point: it easy to miss methods introduced by

> superclasses. Check out the methods of JTextComponent

> -- the superclass of JTextArea. You may find the its

> [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swi

> ng/text/JTextComponent.html#read(java.io.Reader,%20jav

> a.lang.Object)]read()[/url] and

> [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swi

> ng/text/JTextComponent.html#write(java.io.Writer)]writ

> e()[/url] methods darn handy.

ah. they are handy, aren't they. wonder if it's time I read a swing tutorial.....

georgemca at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...
# 8
> ah. they are handy, aren't they. wonder if> it's time I read a swing tutorial.....I am doing that from a few days.Still working with the Layout Managers.Can't think about working with the Swing tutorials without the the tutorials on Layout Managers.
qUesT_foR_knOwLeDgea at 2007-7-8 22:28:32 > top of Java-index,Java Essentials,New To Java...