I'm having some problem with some java code. I'm not very good with java, so this code has been cannibalized from two seperate sources, but I've been spending quite some time trying to get it to work. I have created a class called 'Configuration', and I am trying to save and load a configuration called tcf. This is the code I have now:
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = OpenChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = OpenChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
FileInputStream fis = new FileInputStream(file.getAbsolutePath()+".tcf");
ObjectInputStream ois = new ObjectInputStream(fis);
try {
tcf = (Configuration) ois.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
}
ois.close();
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = SaveChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = SaveChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+".tcf");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tcf);
oos.close();
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
The save code thows an IO exception when I try to run it, though it does manage to create a file. When I try to load the file, it throws another IO exception, but I do not know whether this is because the loading code is bad, or because the file was not saved properly.