在 Java 中,如何检查某物是否是目录,如果不是,则抛出异常?
In Java, how do I check if something is a directory, and if not, throw an exception?
我创建了这个新对象:
File dir = new File(userHome + "//data")
在另一个 class 中,我想验证 'dir' 是一个目录,如果不是,则抛出 IllegalArgumentException。
然后我的目标是在该目录(如果是目录)中找到特定文件类型并处理它们。
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}else{
throw new IllegalArgumentException();
}
new File(path).isDirectory();
如果文件是目录,这将 return 为真。
File dir = new File(userHome + "//data");
if(dir.isDirectory()){
//store files in an array of File
fileArray = dir.listFiles();
}
else
//do whatever you want
我创建了这个新对象:
File dir = new File(userHome + "//data")
在另一个 class 中,我想验证 'dir' 是一个目录,如果不是,则抛出 IllegalArgumentException。
然后我的目标是在该目录(如果是目录)中找到特定文件类型并处理它们。
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}else{
throw new IllegalArgumentException();
}
new File(path).isDirectory();
如果文件是目录,这将 return 为真。
File dir = new File(userHome + "//data");
if(dir.isDirectory()){
//store files in an array of File
fileArray = dir.listFiles();
}
else
//do whatever you want