Pages Navigation Menu

Coding is much easier than you think

How To Check If A File Exists In Java

 
How To Check If A File Exists In Java

To determine whether a file is exist in your file system, use the Java IO File.exists().

 

package com.simplecode.file;
 
import java.io.File;
 
public class DeleteFile
{
    public static void main(String[] args)
    {	
    	try
        {
 
    		File file = new File("c:\file.txt");
 
    		if(file.exists()){
    			System.out.println(file.getName() + " exists!");
    		}else{
    			System.out.println("File does not exists.");
    		}
 
    	}
        catch(Exception e)
        {
            e.printStackTrace();
    	}
 
    }
}

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: