• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 27589
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic