Writing files in Swing

Hi folks, me once again,I'm in the process of writing a simple text editor, and I'm having a great deal of hassle getting the thing to save the contents to a disk. Any help would be greatly appreciated.Emyr
[236 byte] By [Emyra] at [2007-10-3 5:04:17]
# 1

If you're just writing simple text to a file, it's not that hard... You can get the text from a JTextArea (for example) with:

String alltext=textarea.getText();

then write it to a file:

try {

BufferedWriter out=new BufferedWriter(new FileWriter("filename"));

out.write(alltext);

}

catch (IOException e) {

e.printStackTrace();

}

That's it!

Hope that helps :D

Seb

sebmaynarda at 2007-7-14 23:10:11 > top of Java-index,Desktop,Developing for the Desktop...
# 2
I'm more after writing a save dialogue to save the file, but what you've given me is good :D many thanks indeed
Emyra at 2007-7-14 23:10:11 > top of Java-index,Desktop,Developing for the Desktop...
# 3

If you want to use a save dialog, you can do that with the code above:

// ask the user for a filename:

JFileChooser fc=new JFileChooser();

int result=fc.showSaveDialog(this);

if (result==JFileChooser.APPROVE_OPTION) {

// the user pressed ok :)

String filename=fc.getSelectedFile();

// do the filewriting bit from before :)

// ...

}

Hope that helps :)

Seb

sebmaynarda at 2007-7-14 23:10:11 > top of Java-index,Desktop,Developing for the Desktop...