tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > IO > File Existence

File Existence 

The following example shows how to check for the existence of a given file.

File Name  :  
com/bethecoder/tutorials/io/CheckFileExistence.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.io;

import java.io.File;

public class CheckFileExistence {

  /**
   @param args
   */
  public static void main(String[] args) {

    File file = new File("C:\\file_ops.txt");
    
    if (file.exists()) {
      System.out.println(file + " exists");
    else {
      System.out.println(file + " doesn't exist");
    }
    
    file = new File("C:\\missing.txt");
    
    if (file.exists()) {
      System.out.println(file + " exists");
    else {
      System.out.println(file + " doesn't exist");
    }
  }

}
   

It gives the following output,
C:\file_ops.txt exists
C:\missing.txt doesn't exist



 
  


  
bl  br