How to use the Commons IO project to create, delete, and move files and directories

I wrote the following Java class today that is a "helper" class for a project I'm working on to create a web interface for Nagios. The Nagios part isn't too relevant, because mostly what I'm trying to show here is how to use the Apache Commons IO project to make it easier to interact with files and directories from Java code.

In short, the following Java class shows how to use the Commons IO project to accomplish the following tasks. Without any further delay, here is the complete source code for my class:

package com.devdaily.nagios.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;

public class NagiosFileHelper
{
  // the directory in which we will create our subdirectories
  private String nagiosConfigDirectory;

  public void moveConfigFilesFromSourceToTarget(String srcDirName,
         String targetDirName) throws IOException
  {
    File targetDirectory = new File(targetDirName);
    Collection filesToMove = getListOfAllConfigFiles(srcDirName);
    Iterator it = filesToMove.iterator();
    try
    {
      while (it.hasNext())
      {
        File f = (File)it.next();
        FileUtils.moveFile(f, targetDirectory);
      }
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
      throw ioe;
    }
  }

  /**
  * Creates a directory based on the fully-qualified name
  * @param directoryName
  * @return Returns true if the directory was created, 
  * otherwise false.
  */
  public boolean mkdir(String parentDirectory, String directoryName)
  {
    String fullDirectoryPath = parentDirectory + File.separator 
        + directoryName;
    File directory = new File(fullDirectoryPath);
    return directory.mkdir();
  }
 
  public void deleteDirectory(String directoryName) throws IOException
  {
    try
    {
      FileUtils.deleteDirectory(new File(directoryName));
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
      throw ioe;
    }
  }
 
  private Collection getListOfAllConfigFiles(String directoryName)
  {
    File directory = new File(directoryName);
    return FileUtils.listFiles(directory, 
        new WildcardFileFilter("*.cfg"), null);
  }
 
  /**
  * Returns a folder name based on the current date/time, something
  * like "20080725.013755".
  */
  public String getNagiosBackupFolderName()
  {
    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.hhmmss");
    return sdf.format(date);
  }

  public String getNagiosConfigDirectory()
  {
    return nagiosConfigDirectory;
  }

  public void setNagiosConfigDirectory(String nagiosConfigDirectory)
  {
    this.nagiosConfigDirectory = nagiosConfigDirectory;
  }
}

I see in there that I also show an example of how to get today's Date and then create a custom format for it. Hopefully that will help as well.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.