Java.io.File.isFile() Method
Advertisements
Description
The java.io.File.isFile() checks whether the file denoted by this abstract pathname is a normal file.
Declaration
Following is the declaration for java.io.File.isFile() method:
public boolean isFile()
Parameters
NA
Return Value
The method returns true if and only if the file denoted by this abstract pathname is a file else the method returns false.
Exception
SecurityException -- If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file
Example
The following example shows the usage of java.io.File.isFile() method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
String path;
boolean bool = false;
try{
// create new file
f = new File("c:");
// true if the file path is a file, else false
bool = f.isFile();
// get the path
path = f.getPath();
// prints
System.out.println(path+" is file? "+ bool);
// create new file
f = new File("c:/test.txt");
// true if the file path is a file, else false
bool = f.isFile();
// get the path
path = f.getPath();
// prints
System.out.print(path+" is file? "+bool);
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
c: is file? false c:\test.txt is file? true