To perform an operation when clicking a button, you will need code something like this:
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent event) {
fileOperation();
}
}
...
private void fileOperation() {
... do stuff with file ...
}
You can probably find many examples with google. Generally the anonymous inner class code should be short and just translate the event and context into operations meaningful to the outer class.
Currently you need to delete the file manually with File.delete
after you have closed it. If you really wanted to you could extends, say, RandomAccessFile
and override close
to delete after the close. I believe delete-on-close was considered as a mode for opening file on JDK7 (no idea if it is in or not).
Just writing to a file, as in your code, would be pointless. You would presumably want to delete the file after closing a read stream no the write stream. It's not a bad idea to avoid temporary files if you possibly can.