Program
import java.io.*; public class FileExamples { public static void main(String[] args) { File f= null; String path; boolean bool = false; try { f = new File("c:"); bool = f.isDirectory(); path = f.getPath(); System.out.println("1.Boolean isDirectory() : " + path + bool); bool = f.isFile(); path = f.getPath(); System.out.println("2.Boolean isFile() : " + path + bool); bool = f.isHidden(); path = f.getPath(); System.out.println("3.Boolean isHidden() : " + path + bool); bool = f.exists(); path = f.getPath(); System.out.println("4.Boolean exists() : " + path + bool); System.out.println(""); f = new File("c:/test.txt"); bool = f.isDirectory(); path = f.getPath(); System.out.println("1.Boolean isDirectory() : " + path + bool); bool = f.isFile(); path = f.getPath(); System.out.println("2.Boolean isFile() : " + path + bool); bool = f.isHidden(); path = f.getPath(); System.out.println("3.Boolean isHidden() : " + path + bool); bool = f.exists(); path = f.getPath(); System.out.println("4.Boolean exists() : " + path + bool); } catch (Exception e) { e.printStackTrace(); } } }
Output
1.Boolean isDirectory() : c:true 2.Boolean isFile() : c:false 3.Boolean isHidden() : c:false 4.Boolean exists() : c:true 1.Boolean isDirectory() : c:\test.txtfalse 2.Boolean isFile() : c:\test.txtfalse 3.Boolean isHidden() : c:\test.txtfalse 4.Boolean exists() : c:\test.txtfalse
Description
public boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
Where it is required to distinguish an I/O exception from the case that the file is not a directory, or
where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used.
Returns:
true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise
Throws:
SecurityException – If a security manager exists and itsSecurityManager.checkRead(java.lang.String)
method denies read access to the file
public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal file.
A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria.
Where it is required to distinguish an I/O exception from the case that the file is not a normal file, or
where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used.
Returns:
true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
Throws:
SecurityException – If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies
read access to the file
public boolean isHidden()
Tests whether the file named by this abstract pathname is a hidden file.
The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if
its name begins with a period character (‘.’). On Microsoft Windows systems, a file is considered to be hidden if it has
been marked as such in the filesystem.
Returns:
true if and only if the file denoted by this abstract pathname is hidden according to the conventions of the underlying platform
Throws:
SecurityException – If a security manager exists and its
SecurityManager.checkRead(java.lang.String) method denies read access to the file
public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.
Returns:
true if and only if the file or directory denoted by this abstract pathname exists; false otherwise
Throws:
SecurityException – If a security manager exists and its
SecurityManager.checkRead(java.lang.String) method deniesread access to the file or directory