Sunday, August 9, 2015

[Java] File Utility

Below is a sample Java class that can be used as reference when creating a file utility class. The class contain methods for file and folder check, create and delete.


import java.io.File;
import java.io.IOException;

public class FileUtility {

public static boolean isFolderExist(String folder){
   File f = new File(folder);
   return f.exists();
}

public static void createFolder(String folder){
   File f = new File(folder);
   f.mkdir();
     
}

public static boolean isFileExist(String file){
   File f = new File(file);
   return f.exists();
}

public static void createFile(String file){
   File f = new File(file);
   try {
      f.createNewFile();
   } catch (IOException e) {
      e.printStackTrace();
   }
}

public static void deleteFile(String file){
   File f = new File(file);
   f.delete();
}


public static void main(String[] args) {
   String folder = "myfolder";
   System.out.println(">>Is folder exist? ::: " 
      + isFolderExist(folder));
   if(!isFolderExist(folder)){
      createFolder(folder);
   }

}//End main


}//End class

No comments:

Post a Comment