Following example shows how to check file existence using file.exists() method in Java.
exists
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 denies read access to the file or directory
Sample Program
CheckFileExist.java
Result
The above code will produce the following output if file exist in specified directory.
exists
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 denies read access to the file or directory
Sample Program
CheckFileExist.java
package com.javatutorialcorner.file; import java.io.File; public class CheckFileExist { public static void main(String[] args) { File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt"); System.out.println(jtcFile.exists()); } }
Result
The above code will produce the following output if file exist in specified directory.
true
0 comments:
Post a Comment