Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to Check or Delete Directory in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Rob Dizzle's Avatar
Member
 
Join Date: Aug 2006
Posts: 277
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
Reply With Quote
  #2  
Old 19-02-2010
Deabelos's Avatar
Member
 
Join Date: Jul 2006
Posts: 233
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....
Reply With Quote
  #3  
Old 19-02-2010
MELTRONICS's Avatar
Member
 
Join Date: Aug 2006
Posts: 226
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
Reply With Quote
  #4  
Old 19-02-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
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;
}
Reply With Quote
  #5  
Old 19-02-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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.
Reply With Quote
  #6  
Old 19-02-2010
Allan.d's Avatar
Member
 
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);
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Check or Delete Directory in Java?"
Thread Thread Starter Forum Replies Last Post
Batch file to check directory Trini Alvarado Software Development 5 01-04-2010 12:00 PM
How to check if file exists in directory with Php Zool Software Development 3 03-11-2009 12:36 PM
Active Directory Forest Health Check answrman Active Directory 5 20-02-2009 07:07 PM
Powershell: To check a directory exists or not? Chandrakant81 Software Development 3 18-02-2009 06:26 PM
A health-check script for Active Directory on Win 2000/2003 JackFlash Active Directory 5 19-02-2007 02:44 PM


All times are GMT +5.5. The time now is 04:07 AM.