1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* 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. */ |
Add Comment