Description:
In this section we will be tackling on how to check a file if it exists or not
Check file exists in java
Java File class have a method exists() from a give filename declared in the constructor of this class. The exists method return a boolean value, true if the file or folder exists or not.
Sample Source code:
package com.javatutorialhq.tutorial;
import java.io.File;
import java.io.IOException;
public class JavaFileCheckExistence {
/**
* This java sample code shows how check exists or not
* Property of javatutorialhq.com
* All Rights Reserved
* Version 1.0 06/13/2012
*/
public static void main(String[] args) throws IOException {
File f = new File("C:\\temp\\testFolder\\testFile.zip");
if (f.exists()) {
System.out.println("File " + f.getCanonicalPath() + " exists");
} else {
System.out.println("Error, file does not exists");
}
}
}
Sample output:
File C:\temp\testFolder\testFile.zip exists
This java source code just basically checks if the file exists or not. Prints the result and exit.