FileHelper.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2008, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.util;

  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.InputStreamReader;
  29. import java.net.MalformedURLException;
  30. import java.nio.channels.FileChannel;
  31. import java.util.ArrayList;
  32. import java.util.List;

  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import org.xml.sax.InputSource;

  36. /**
  37.  * Utility that provides some general methods for working with {@link File} objects.
  38.  *
  39.  * @author gommma
  40.  * @version $Revision$
  41.  * @since 2.3.0
  42.  */
  43. public class FileHelper
  44. {
  45.     private static Logger logger = LoggerFactory.getLogger(FileHelper.class);

  46.     private FileHelper(){
  47.     }
  48.    
  49.     /**
  50.      * Recursively deletes the given directory
  51.      * @param directory The directory to delete
  52.      * @param failOnError If an exception should be thrown in case the deletion did not work.
  53.      */
  54.     public static void deleteDirectory(File directory, boolean failOnError) {
  55.         boolean success = deleteDirectory(directory);
  56.         if(!success){
  57.             throw new RuntimeException("Failed to delete directory " + directory);
  58.         }
  59.     }

  60.     /**
  61.      * Recursively deletes the given directory
  62.      * @param directory The directory to delete
  63.      * @return <code>true</code> if the deletion was successfully.
  64.      */
  65.     public static boolean deleteDirectory(File directory)
  66.     {
  67.         if(!directory.isDirectory()) {
  68.             logger.warn("The directory '" + directory + "' does not exist. Will return without delete.");
  69.             return false;
  70.         }
  71.        
  72.         // First we must delete all files in the directory
  73.         File[] containedFiles = directory.listFiles();
  74.         for (int i = 0; i < containedFiles.length; i++) {
  75.             File currentFile = containedFiles[i];
  76.             if(currentFile.isDirectory()) {
  77.                 // First delete children recursively
  78.                 deleteDirectory(currentFile);
  79.             }
  80.             else {
  81.                 // Delete the file itself
  82.                 boolean success = currentFile.delete();
  83.                 if(!success){
  84.                     logger.warn("Failed to delete file '" + currentFile + "'");
  85.                 }
  86.             }      
  87.         }
  88.         // Finally delete the directory itself
  89.         boolean success = directory.delete();
  90.         if(!success){
  91.             logger.warn("Failed to delete file '" + directory + "'");
  92.         }
  93.         return success;
  94.     }

  95.     public static InputSource createInputSource(File file) throws MalformedURLException
  96.     {
  97.         String uri = file/*.getAbsoluteFile()*/.toURI().toURL().toString();
  98.         InputSource source = new InputSource(uri);
  99.         return source;
  100.     }
  101.    
  102.    
  103.     /**
  104.      * Copy file.
  105.      *
  106.      * @param srcFile the src file
  107.      * @param destFile the dest file
  108.      * @throws IOException
  109.      */
  110.     public static void copyFile(File srcFile, File destFile) throws IOException
  111.     {
  112.         logger.debug("copyFile(srcFile={}, destFile={}) - start", srcFile, destFile);

  113.         // Create channel on the source
  114.         FileChannel srcChannel = new FileInputStream(srcFile).getChannel();

  115.         // Create channel on the destination
  116.         FileChannel dstChannel = new FileOutputStream(destFile).getChannel();

  117.         try {
  118.             // Copy file contents from source to destination
  119.             dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
  120.         }
  121.         finally {
  122.             // Close the channels
  123.             srcChannel.close();
  124.             dstChannel.close();
  125.         }
  126.     }

  127.     /**
  128.      * Get a list of Strings from a given file.
  129.      * Uses the default encoding of the current platform.
  130.      *
  131.      * @param theFile the file to be read
  132.      * @return a list of Strings, each one representing one line from the given file
  133.      * @throws IOException
  134.      */
  135.     public static List readLines(File theFile) throws IOException
  136.     {
  137.         logger.debug("readLines(theFile={}) - start", theFile);

  138.         InputStream tableListStream = new FileInputStream(theFile);
  139.         try {
  140.             List orderedNames = new ArrayList();
  141.             BufferedReader reader = new BufferedReader(new InputStreamReader(tableListStream));
  142.             String line = null;
  143.             while ((line = reader.readLine()) != null) {
  144.                 String table = line.trim();
  145.                 if (table.length() > 0) {
  146.                     orderedNames.add(table);
  147.                 }
  148.             }
  149.             return orderedNames;
  150.         }
  151.         finally {
  152.             tableListStream.close();
  153.         }
  154.     }

  155. }