An example on checking whether a file is directory or a normal file in Java using Java IO.
Any of the two methods can be used.
Check now!
Output
Any of the two methods can be used.
Java Tutorial, Java Study, Learn Java, Java Lab@home, Java Cycle test, Java Projects, Java Assignments, Java Notes and much more..
import java.io.*;
class CheckFile
{
public static void main(String args[])
{
// Create a file object for a [directory (here)]
File file=new File("C:/java");
// If file is a directory
if(file.isDirectory())
// Print then.
System.out.println(file.getName()+" is a directory");
// If not a directory, then obviously a file
else System.out.println(file.getName()+" is a file");
/* You can alternatively use.. */
// Check if file object represents a normal file
if(file.isFile())
// Print then.
System.out.println(file.getName()+" is a file");
// If not a file, then will be a dir
else System.out.println(file.getName()+" is a directory");
}
}
java is a directory
java is a directory
No comments:
Post a Comment