Finding out if a folder exists

  • Thread starter christopher_board
  • Start date
C

christopher_board

I am currently writing a java application that backs up a directory
and copies it to another location. I want the user to enter the path
of the directory in a JTextField. For example C:\Documents and
Settings. Once the user has entered the path they want to backup they
then click a button which will then check to make sure that, that
directory exists. If it doesn't a message is displayed and if it does
then it performs a certain task. How would I go about checking to make
sure the folder exists.

Thanks very much for your help.

I look forward to hearing from you in the near future.
 
S

Steve W. Jackson

I am currently writing a java application that backs up a directory
and copies it to another location. I want the user to enter the path
of the directory in a JTextField. For example C:\Documents and
Settings. Once the user has entered the path they want to backup they
then click a button which will then check to make sure that, that
directory exists. If it doesn't a message is displayed and if it does
then it performs a certain task. How would I go about checking to make
sure the folder exists.

Thanks very much for your help.

I look forward to hearing from you in the near future.

First, I think your text field should be accompanied by a button (we use
one labeled Browse in similar situations) that supports selecting
(presumably via JFileChooser) directories only. But once you've got a
selected item, even if manually entered, you want to make a File object
out of it and then call the isDirectory method on that object. This
method will return true if the item actually exists and it's a directory
rather than a file. You may want to see if your target directory can be
written to, also.

I highly recommend the API Javadocs on java.io.File for your reading
pleasure. :)

= Steve =
 
E

Eric Sosman

I am currently writing a java application that backs up a directory
and copies it to another location. I want the user to enter the path
of the directory in a JTextField. For example C:\Documents and
Settings. Once the user has entered the path they want to backup they
then click a button which will then check to make sure that, that
directory exists. If it doesn't a message is displayed and if it does
then it performs a certain task. How would I go about checking to make
sure the folder exists.

File theDir = new File(thePath);
if (theDir.exists()) ...

However, I'd encourage you to use a JFileChooser
instead of making the user type the path. If you were
in the user's position, would you rather click on a
couple of folder icons or type

C:\Documents and Settings\Christopher\My Documants\Financial
Records\Mortgage Payments

.... and then curse at the error message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top