Determine File or Directory
- /*
- Determine if it's a File or Directory
- This Java example shows how to determine if a particular file object denotes a
- file or directory of filesystem using isFile and isDirectory
- methods of Java File class.
- */
- import java.io.*;
- public class DetermineDirOrFile {
- public static void main(String[] args) {
- //create file object
- File file = new File("C://FileIO");
- /*
- * To check whether File object denotes a file or not, use
- * boolean isFile() method of Java File class.
- *
- * This method returns true if the file EXISTS and its a normal file.
- */
- boolean isFile = file.isFile();
- if(isFile)
- System.out.println(file.getPath() + " is a file.");
- else
- System.out.println(file.getPath() + " is not a file.");
- /*
- * To check whether File object denotes a directory or not, use
- * boolean isDirectory() method of Java File class.
- *
- * This method returns true if the directory EXISTS and its a directory.
- */
- boolean isDirectory = file.isDirectory();
- if(isDirectory)
- System.out.println(file.getPath() + " is a directory.");
- else
- System.out.println(file.getPath() + " is not a directory.");
- }
- }
- /*
- Output would be
- C:\FileIO is not a file.
- C:\FileIO is a directory.
- */



