Results 1 to 5 of 5
- 12-06-2009, 10:10 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
[SOLVED]FileChooser checking for an existing file
Hi everyone,
I was taking a look at the Java Swing Tutorial and I implemented a FileChooser for saving files. However, I didn't find how to check if the file you are trying to save already exists in the directory. Is there a way to do it?Last edited by sky; 12-07-2009 at 11:29 AM.
-
- 12-06-2009, 10:51 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
I didn't realize that the filechooser only returns the file object, but it doesn't actually save it.
-
- 12-06-2009, 11:06 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
Yes, I've already had the code for that. For creating a FileChooser to save a file and check if the file exists, the code would be something like that (just in case somebody needs it):
Java Code:JFileChooser fc = new JFileChooser(); int returnVal = fc.showSaveDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION){ File file = fc.getSelectedFile(); if (file.exists()) { int n = JOptionPane.showConfirmDialog(frame, "The given file does already exists \n Would you like to replace it?", "File found error", JOptionPane.YES_NO_OPTION); } if (n==JOptionPane.YES_OPTION) { String fileName = file.getName(); // Save the file } else if n==JOptionPane.NO_OPTION) { // Do nothing } } else { // Save the file } }Last edited by sky; 12-06-2009 at 11:08 PM.
Similar Threads
-
checking file extensions
By gisler in forum New To JavaReplies: 3Last Post: 12-05-2009, 01:25 PM -
Changing Existing Zip file using java
By narayanan.1985 in forum Advanced JavaReplies: 1Last Post: 09-30-2009, 08:27 PM -
Checking if file exists in a directory
By achilles in forum New To JavaReplies: 7Last Post: 08-10-2009, 02:43 AM -
show only PNG file in FileChooser
By rabbia in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:04 AM -
Run J2ME application using existing jar file
By chale in forum CLDC and MIDPReplies: 0Last Post: 08-25-2008, 02:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks