Saturday, March 24, 2012

Some Useful Java File Operations and Utility methods

File Modification, Copy, delete and create new file is quite easy in java programming. Here I wrote some java file operation utility method that might be useful for your application.  

Java File Copy Method
public static void copyfile(String srFile, String dtFile) throws Exception {

File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);

// For Append the file.
// OutputStream out = new FileOutputStream(f2,true);

// For Overwrite the file.
OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {

 out.write(buf, 0, len);
}
in.close();
out.close();

}

Java Delete File Method
public static void removeFile(String fileName) throws Exception {

File file = new File(fileName);

if (file.exists()) {
 boolean success = file.delete();
 if (!success) {
  throw new Exception("Fail to delete existing file");
 }
}

}

Java Rename File Method
public static void renameFile(String oldName, String newName) throws IOException {
   File srcFile = new File(oldName);
   boolean bSucceeded = false;
   try {
       File destFile = new File(newName);
       if (destFile.exists()) {
           if (!destFile.delete()) {
               throw new IOException(oldName + " was not successfully renamed to " + newName);
           }
       }
       if (!srcFile.renameTo(destFile))        {
           throw new IOException(oldName + " was not successfully renamed to " + newName);
       } else {
               bSucceeded = true;
       }
   } finally {
         if (bSucceeded) {
               srcFile.delete();
         }
   }
}

Java List of files in specified directory with filter option
  
List fileList = FileUtil.dirlist(sourceLocation, "*.csv");

 public static List dirlist(String path, String filter) throws FileNotFoundException {

   File dir = new File(path);
   
   FileFilter fileFilter = new WildcardFilter(filter);
    File[] chld = dir.listFiles(fileFilter);  
   
   List fileList = new ArrayList();
   
   if (chld == null) {
    log.error("Specified directory does not exist or is not a directory.");
    throw new FileNotFoundException("Specified directory does not exist or is not a directory.");
    
    
   } else {
    for (int i = 0; i < chld.length; i++) {
     File file = chld[i];
     
     if(file.isFile()) {
      fileList.add(file);
     }
     
    }
   }
   return fileList;
  }

Java List of files in specified directory
public static List dirlist(String path) throws FileNotFoundException {
  File dir = new File(path);
  File[] chld = dir.listFiles();
  List fileList = new ArrayList();
  
  if (chld == null) {
   log.error("Specified directory does not exist or is not a directory.");
   throw new FileNotFoundException("Specified directory does not exist or is not a directory.");
   
  } else {
   for (int i = 0; i < chld.length; i++) {
    File file = chld[i];
    
    if(file.isFile()) {
     fileList.add(file);
    }
    
   }
  }
  return fileList;
 }

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

No comments:

Post a Comment