Because read/write operation could be a really long task you should execute it in a different thread to not block the main UI thread.
SwingWorker class can be helpful, because it's created to handle such long-time tasks. Have a look here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html
If you use SwingWorker than instead of your flag you should call cancel method of SwingWorker when someone will press your button.