Hi, i'm making a texteditor in java but i have a problem with the save function.
I'm using Netbeans and the build in GUI editor.
I can save my textfile but only in another folder then my active folder. When i try to save in my current folder, it trows an IOException.
I use a menu button to save, and it calls a jFileChooser to select the correct file.
This is my code for the save function:
|
Code:
|
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = jFileChooser2.showSaveDialog(this);
if (returnVal == jFileChooser2.APPROVE_OPTION)
{
File fout = jFileChooser2.getSelectedFile();
String text=jTextArea1.getText();
String filename;
filename=fout.getPath();
boolean exists=(new File(filename)).exists();
if(!exists)
{System.out.println("Bestaat niet");
new File(filename);
}
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(text);
out.close();
}
catch (IOException e)
{
jOptionPane1.showMessageDialog(null,"Fout bij opslaan van bestand.");
}
}
} |
Any ideas why it won't save my textfile in the current directory?