info.aduna.io
Class FileUtil

java.lang.Object
  extended by info.aduna.io.FileUtil

public class FileUtil
extends Object

Utility methods for operations on Files.


Constructor Summary
FileUtil()
           
 
Method Summary
static void copyFile(File source, File destination)
          Copies the contents of file source to file destination.
static void createDirIfNotExists(File dir)
          Creates a directory if it doesn't exist yet.
static File createTempDir(String prefix)
          Creates a new and empty directory in the default temp directory using the given prefix.
static boolean deleteChildren(File directory)
          Deletes all files and directories in the specified directory.
static void deleteDir(File dir)
          Deletes the specified diretory and any files and directories in it recursively.
static boolean deleteFiles(File directory)
          Deletes all files and directories in the specified directory.
static boolean deltree(File directory)
          Deletes the given file and everything under it.
static String getFileExtension(String fileName)
          Gets the extension of the specified file name.
static File getRelativeFile(File subj, File relativeTo)
          Gets the relative representations of a file compared to another.
static String getRelativePath(File subj, File relativeTo)
          Gets the relative representations of a file compared to another.
static boolean isLegalFileName(String fileName)
          Checks whether the specified file name is a legal (DOS/Windows-) file name.
static boolean isLegalFileNameChar(char c)
          Checks whether the specified character is a legal (DOS/Windows-) file name character.
static boolean moveRecursive(File from, File to, Collection<File> excludes)
          Moves the given file and all files under it (if it's a directory) to the given location, excluding the given collection of File objects!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

getRelativePath

public static String getRelativePath(File subj,
                                     File relativeTo)
Gets the relative representations of a file compared to another.

Parameters:
subj - The File to find the relative form for.
relativeTo - The File 'subj' should be made relative to.
Returns:
The relative representation of subj.

getRelativeFile

public static File getRelativeFile(File subj,
                                   File relativeTo)
Gets the relative representations of a file compared to another.

Parameters:
subj - The File to find the relative form for.
relativeTo - The File 'subj' should be made relative to.
Returns:
The relative representation of subj.

getFileExtension

public static String getFileExtension(String fileName)
Gets the extension of the specified file name.

Parameters:
fileName - A file name.
Returns:
The file name extension (e.g. "exe" or "txt"), or null if the file name does not have a (valid) extension.

isLegalFileName

public static boolean isLegalFileName(String fileName)
Checks whether the specified file name is a legal (DOS/Windows-) file name.


isLegalFileNameChar

public static boolean isLegalFileNameChar(char c)
Checks whether the specified character is a legal (DOS/Windows-) file name character.


copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
Copies the contents of file source to file destination.

Throws:
IOException

createDirIfNotExists

public static void createDirIfNotExists(File dir)
                                 throws IOException
Creates a directory if it doesn't exist yet.

Parameters:
dir - The directory to create.
Throws:
IOException - If the creation of the directory failed.

deltree

public static boolean deltree(File directory)
Deletes the given file and everything under it.

Returns:
Whether all files were deleted succesfully.

deleteFiles

public static boolean deleteFiles(File directory)
Deletes all files and directories in the specified directory. Nothing happens when the specified File is not a directory.

Returns:
true when all files in the specified directory were successfully deleted, when there where no files or when the specified file was not a directory.

deleteChildren

public static boolean deleteChildren(File directory)
Deletes all files and directories in the specified directory. Nothing happens when the specified File is not a directory.

Returns:
true when all children were successfully deleted, when there were no children or when the file was not a directory.

moveRecursive

public static boolean moveRecursive(File from,
                                    File to,
                                    Collection<File> excludes)
Moves the given file and all files under it (if it's a directory) to the given location, excluding the given collection of File objects!

Parameters:
from - File or directory to be moved
to - The file or directory to rename to
excludes - The File objects to be excluded; if a directory is excluded, all files under it are excluded as well!
Returns:
Whether moving was succesfull

createTempDir

public static File createTempDir(String prefix)
                          throws IOException
Creates a new and empty directory in the default temp directory using the given prefix. This methods uses File.createTempFile(java.lang.String, java.lang.String, java.io.File) to create a new tmp file, deletes it and creates a directory for it instead.

Parameters:
prefix - The prefix string to be used in generating the diretory's name; must be at least three characters long.
Returns:
A newly-created empty directory.
Throws:
IOException - If no directory could be created.

deleteDir

public static void deleteDir(File dir)
                      throws IOException
Deletes the specified diretory and any files and directories in it recursively.

Parameters:
dir - The directory to remove.
Throws:
IOException - If the directory could not be removed.


Copyright © 2011 Aduna. All Rights Reserved.