by @kodeazy

Java How to check file is created successfully and present in the file path?

Home » java » 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 then File is present is executed else File 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");	
    	}
    	}
    }