Skip to content
Advertisement

How To Open File Dialog And Create File On It?

1

I opened File Dialog but I don’t create the file on it? How?

JavaScript

Advertisement

Answer

To save a file with JFileChooser, you need to use the showSaveDialog() method instead of the showOpenDialog() like in your snippet. For more information check out How to use File Choosers and check out the JFileChooser JavaDoc.

Then the next step if the saving has been approved, is to actually write the file. For this, you can use a FileWriter.

I put together a small snippet, which opens a JFileChooser on a button click, where you can provide the filename, where some String will be written to this file.

Example:

JavaScript

Output:

enter image description here

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement