0

I've created this new object:

File dir = new File(userHome + "//data")

In another class, I want verify that 'dir' is a directory and if it isn't, throw an IllegalArgumentException.

My goal is to then locate specific file types in that directory (if a directory) and process them.

gppanter
  • 333
  • 1
  • 6
  • 19

2 Answers2

4
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
   ...
}else{
   throw new IllegalArgumentException();
}
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
igon
  • 3,016
  • 1
  • 22
  • 37
0
new File(path).isDirectory(); 

This will return true if the File is a Directory.

File dir = new File(userHome + "//data");
if(dir.isDirectory()){
   //store files in an array of File
   fileArray = dir.listFiles();
}
else 
 //do whatever you want
Nicolas El Khoury
  • 5,867
  • 4
  • 18
  • 28