by @kodeazy
Java How to check file is created successfully and present in the file path?
- In this blog we will be discussing on how to check File is created or not in the path specified inside
File
in java. - To check file exist or not we use
file.exists()
function. -
Below is the syntax.
if(file.exists()) { System.out.println("File is present"); } else { System.out.println("File is Not present"); }
- In the above syntax if file is present in the
file
object thenFile is present
is executed elseFile is Not present
is executed. -
Below is the sample example.
import java.io.File; import java.io.IOException; import java.nio.file.Path; class FileExample{ public static void main(String args[]) throws Exception{ File file = new File("fileExample.txt"); file.createNewFile(); if(file.exists()) { System.out.println("File is present"); } else { System.out.println("File is Not present"); } } }