exists(): This method is available in the java.io.File class of Java.
Syntax:
boolean java.io.File.exists()
This method tests whether the file or directory denoted by this abstract path name exists.
Parameters: NA
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 java.lang.SecurityManager.checkRead(java.lang.String) method denies read access to the file or directory.
Approach 1: When the file is not present
Java
import java.io.File;public class Fileexists {public static void main(String[] args) {String pathname = "D:\\hello.txt";File file = new File(pathname);System.out.println(file.exists());}}
Output:
false
Approach 2: When the file is present
Java
import java.io.File;public class Fileexists {public static void main(String[] args) {String pathname = "D:\\hello.txt";File file = new File(pathname);System.out.println(file.exists());}}
Output:
true
No comments:
Post a Comment