Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Finding out if a folder exists

Thread view: 
christopher_board@yahoo.co.uk - 05 Feb 2007 17:42 GMT
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 - 05 Feb 2007 17:57 GMT
> 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
[quoted text clipped - 8 lines]
>
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Eric Sosman - 05 Feb 2007 18:07 GMT
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
[quoted text clipped - 4 lines]
> 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?

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.