hi, how are you all doing?
why does my JFileChooser not write to the file all it does is create it, here is my code, thanks for your help.
SaveAFile(myJFrame f)
{
this.f = f;
String fileName = "";
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(f);
if(result==JFileChooser.CANCEL_OPTION)
JOptionPane.showMessageDialog(f,"you have selected cancel",
"No file saved",JOptionPane.ERROR_MESSAGE);
else if(result == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
Save(getContentsOfFile(file),fileChooser.getSelectedFile());
}
}
void Save(String s , File f)
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(f));
out.write(s);
out.close();
}
catch(IOException e)
{
e.toString();
}
}
String getContentsOfFile(File file)
{
String s = new String();
int fileLength = (int)file.length();
char buffer[] = new char[fileLength];
InputStreamReader input;
try
{
int index;
input = new FileReader(file);
while((index=input.read(buffer,0,buffer.length))!=-1)
s = s + new String(buffer,0,index);
}
catch(IOException e)
{
e.toString();
}
return s;
}