I wrote this short program which has a tiny GUI. Its supposed to allow you to type text and then you can save it to a txt file or read from a previously saved file or delete an existing file.
It works, but with some strange bugs. Sometimes after a file is created, if I try to delete it, it will not delete the file.
Also, Im totally not sure if I wrote the code in a good way.
I was wondering if you can have a look at what I wrote and find weaknesses or areas that I should write differently. And also maybe you can see why there is a problem with deleting the file after it was created. I’m really at a loss and I don’t know who to ask.
Here is the code I have:
public class InputOutout extends JFrame{
private static final long serialVersionUID = -7073762217756427192L;
JLabel label;
JTextField tf;
JButton buttonAdd;
JButton buttonDisplay;
JButton buttonErase;
public InputOutout() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setLayout(new FlowLayout());
label = new JLabel("Enter text");
add(label);
tf = new JTextField(10);
add(tf);
buttonAdd = new JButton("Press to save to file");
add(buttonAdd);
buttonDisplay = new JButton("Press to display file content");
add(buttonDisplay);
buttonErase = new JButton("Press to erase file");
add(buttonErase);
eventAdd add = new eventAdd();
eventDisplay remove = new eventDisplay();
eventErase erase = new eventErase();
buttonAdd.addActionListener(add);
buttonDisplay.addActionListener(remove);
buttonErase.addActionListener(erase);
}
private class eventAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String word = tf.getText()+ " ";
FileWriter stream = new FileWriter("text.txt",true);
BufferedWriter out = new BufferedWriter(stream);
out.append(word);
out.close();
tf.setText("");
} catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
private class eventDisplay implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
FileReader stream = new FileReader("text.txt");
BufferedReader in = new BufferedReader(stream);
String text = in.readLine();
JOptionPane.showMessageDialog(null, text);
stream.close();
} catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
private class eventErase implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("text.txt");
boolean success = file.delete();
if (!success) {
JOptionPane.showMessageDialog(null, "File was not deleted");
} else {
JOptionPane.showMessageDialog(null, "File was deleted");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
public static void main(String[] args) {
InputOutout gui = new InputOutout();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(350,100);
gui.setTitle("SuperWORD 2013");
gui.setVisible(true);
}
}
The logic surrounding the
file.delete()is not quite correct, astruewill only be returned if it is deleted. This means thatfile.delete()will returnfalseif it does not exist. Check if the file exists before attempting to delete it:Recommend using a
try {} catch() finally {}construct to ensureBufferedReader.close(), orBufferedWriter.close()is called: