Results 1 to 6 of 6

Thread: How to Check or Delete Directory in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    287

    How to Check or Delete Directory in Java?

    I have just started some basic things of I/O in Java programming language. I have not understood the methods for checking or deleting the files or directories. So thought that you guys will explain me much better. I am talking about the cases which are related to the I/O (input/output) of java.io package. So please tell me how to Check or Delete Directory in Java.?? Expecting some good help sooner.!!
    Dimension 1100 (FMY032J) mini-tower
    2.53ghz Intel Pentium 4
    80 gig nfts HDD
    512 RAM
    Main circuit board: Dell 0CF458
    BIOS: Dell A00
    Display: Intel(R) 82865G Graphics Controller [Display adaptor]
    Multimedia: Sound MAX Integrated Digital Audio
    Windows XP Home SP2

  2. #2
    Join Date
    Jul 2006
    Posts
    286

    Re: How to Check or Delete Directory in Java?

    You can use the checkAccess(AccessMode) method, if you want to verify that a file exists and that the program can access it as needed. The varargs argument can be any combination of the following AccessMode options :
    1. Read - This option can be used for checking that the file exists and that the program has permission to read the file.
    2. Write - By using this option you can check that the file exists and that the program has permission to write to the file.
    3. Execute - This option checks that the file exists and that the program has permission to execute the file.
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  3. #3
    Join Date
    Aug 2006
    Posts
    235

    Re: How to Check or Delete Directory in Java?

    I have provided you with the sample of coding that explains to verify the particular file exists and that the program has the ability to execute the file. The code I have used is for the Path which is a file and not a directory. Just look at the coding :
    Code:
    import static java.nio.file.AccessMode.*;
      
    Path file = ...;
    try {
        file.checkAccess(READ, EXECUTE);
    } catch (IOException x) {
        return;
    }
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to Check or Delete Directory in Java?

    You can also check whether two paths locate the same file. You will have to use the isSameFile(Path) method that can compare two paths to determine if they locate the same file on the file system. For doing that you can use the following sample of the code :
    Code:
    Path pa1 = ...;
    Path pa2 = ...;
    
    try {
        if (pa1.isSameFile(pa2)) {
        }
    } catch (IOException x) {
        return;
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Check or Delete Directory in Java?

    You can delete files, directories or links. You must keep in mind while deleting the symbolic links, the link is deleted and not the target of the link. Two deletion methods are provided by the Path class :
    • delete
    • deleteIfExists
    The delete method deletes the file or throws an exception if the deletion fails. While the deleteIfExists method also deletes the file, but if the file does not exist, no exception is thrown.

  6. #6
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Check or Delete Directory in Java?

    I think that catching an exception is always useful. If you want to catch the exception to determine why the delete failed, you will have to write the code as below :
    Code:
    try {
        path.delete();
    } catch (NoFileException x) {
        System.err.format("%s: no mentioned file or directory%n", path);
    } catch (DirectoryNotEmptyException x) {
        System.err.format("%s not empty%n", path);
    } catch (IOException x) {
        System.err.println(x);
    }

Similar Threads

  1. Batch file to check directory
    By Trini Alvarado in forum Software Development
    Replies: 5
    Last Post: 01-04-2010, 11:00 AM
  2. How to check if file exists in directory with Php
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:36 PM
  3. Active Directory Forest Health Check
    By Robbin M in forum Active Directory
    Replies: 3
    Last Post: 19-02-2009, 03:43 AM
  4. Powershell: To check a directory exists or not?
    By Chandrakant81 in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 06:26 PM
  5. A health-check script for Active Directory on Win 2000/2003
    By JackFlash in forum Active Directory
    Replies: 5
    Last Post: 19-02-2007, 02:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,706,662.49629 seconds with 17 queries