aspose file tools*
The moose likes Beginning Java and the fly likes Looking at a Directory for existence of files (with any extension) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looking at a Directory for existence of files (with any extension)" Watch "Looking at a Directory for existence of files (with any extension)" New topic
Author

Looking at a Directory for existence of files (with any extension)

Mei Jones
Greenhorn

Joined: Oct 19, 2012
Posts: 8
In following code I am expecting Java to make exists = "True" if there is any .doc, .xls, .pdf and many more files.

But it is checking only subdirectories inside the main folder.

What class I should instantiate object "f" from?



boolean exists = f.exists();



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So if your object corresponds to the directory /A/B/C and you want to list all of C's peers--for example, A/B/D.txt, /A/B/E.doc, etc., then you don't want to call list() on C, you want to call it on B. File has a method called getParent() or something like that which gives you the parent directory of the current File object, which is where you want to call list() (if I understand your problem correctly).
Mei Jones
Greenhorn

Joined: Oct 19, 2012
Posts: 8
Thanks for the reply..

All what I need by checking exists on File object whether the last sub folder is empty or not.

/A/B/C .. if C is the last folder want to know whether C is empty or not. I thought f.exists() will do the trick apparently not..

I like any light on this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you want to know whether a folder is empty, the way to do that is to get the files in that folder into an array, using one of the methods of the File class. If that array is empty, that means the directory is empty.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mei Jones wrote:Thanks for the reply..

All what I need by checking exists on File object whether the last sub folder is empty or not.

/A/B/C .. if C is the last folder want to know whether C is empty or not. I thought f.exists() will do the trick apparently not..


If your File object corresponds to /A/B/C, then exists() will tell you whether /A/B/C exists. This is stated in the documentation.

If you want to know if C is empty, then you need to to call isDirectory(), and if it's true, call list() or listFiles() and see if it returns an empty array. This is also evident from the documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looking at a Directory for existence of files (with any extension)