Program to Find file exists or not - in java file IO





java.io.File's exists() method can to find whether File exists or not
Method returns true  -  if File exists
Method returns false - if File doesn't exists





Program to find File exists or not - in java file IO >

import java.io.File;
/** JavaMadeSoEasy.com */
public class FileExists {
   public static void main(String... args) {
          String fileName = "c:/myFile.txt";
          File file = new File(fileName);
         
          boolean fileExists = file.exists();
         
          if(fileExists)
                 System.out.println(fileName +" exists");
          else
                 System.out.println(fileName +" doesn't exists");
         
   }
}
/* OUTPUT
c:/myFile.txt exists
*/



RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program on How to hide and unHide file or Directory in java 7 using java.nio.file




Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Copy a Directory


Traverse a Directory (all sub-directories and files) in 2 ways




No comments:

Post a Comment