Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Finding out if a folder exists (http://www.velocityreviews.com/forums/t390632-finding-out-if-a-folder-exists.html)

christopher_board@yahoo.co.uk 02-05-2007 05:42 PM

Finding out if a folder exists
 
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.


Steve W. Jackson 02-05-2007 05:57 PM

Re: Finding out if a folder exists
 
In article <1170697362.596760.285540@v33g2000cwv.googlegroups .com>,
christopher_board@yahoo.co.uk wrote:

> 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 =
--
Steve W. Jackson
Montgomery, Alabama

Eric Sosman 02-05-2007 06:07 PM

Re: Finding out if a folder exists
 
christopher_board@yahoo.co.uk wrote On 02/05/07 12:42,:
> 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?

--
Eric.Sosman@sun.com


All times are GMT. The time now is 11:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57