That looks like too much code for an ActionListener. It also looks as though you have a book which teaches that abomination against object‑oriented programming,
addActionListener(this);
I suggest once you have got the method working from the command line, using a call like this, you create a Listener class
Note:-
1: I don’t like main methods more than 1 statement long, but I shall make an exception and deprecate the method from further use.2: I have assumed you are overloading your method so you pass the details of the files, data to write, etc as arguments.3: You would do well to try out things like file choosers for finding files before writing the Listener.4: There are several ways to write Listener classes, which you can find about by searching in our GUIs forum. This is a rough‑and‑ready example of one way to do it.I can try duplicating this
thread into the GUIs forum; that might get you more attention.