org.jtheque.utils.io
Class FileUtils

java.lang.Object
  extended by org.jtheque.utils.io.FileUtils

public final class FileUtils
extends Object

An utility class for file manipulation.

Author:
Baptiste Wicht

Method Summary
static InputStream asInputStream(String path)
          Return an input stream to the path.
static void clearFolder(File folder)
          Clear the folder.
static void close(Closeable closable)
          Close a closable resource.
static void close(ZipFile zipFile)
          Close a zip file.
static void copy(File source, File target)
          This method copy a source's file to a destination's file.
static void copy(String sourcePath, String targetPath)
          Copy a file.
static void createEmptyFile(String path)
          Create an empty file.
static void createIfNotExists(File file)
          Create the file if not exists.
static void delete(File file)
          Delete a file.
static void delete(String path)
          Delete a file.
static void downloadFile(String filePath, String destination)
          Download a file.
static String encryptKey(String key)
          Encrypt the string with the SHA-256 Algorithm.
static File getAnExistingFile()
          Return an existing file.
static long getFileSize(File file)
          Return the size of the specified file.
static long getFileSize(String path)
          Return the size of the file specified by the path.
static Collection<File> getFilesOfFolder(File folder, FileFilter fileFilter)
          Return all the files of the folder include the sub files and folders with no level limit.
static String getFreeName(String folder, String name)
          Return the next free name for the specified name in the specified folder.
static Date getLastModifiedDate(File file)
          Return the date of the last modification of the specified file.
static Date getLastModifiedDate(String path)
          Return the date of the last modification of the file denoted by the specified path.
static Collection<String> getLinesOf(InputStream stream)
          Return the lines of a stream.
static String getTextOf(String path)
          Return the text of the file referenced by the specified path.
static String getTextOfSystemResource(String path)
          Return the text of the file.
static boolean isFileInDirectory(File file, File folder)
          Test if a file is in a directory.
static void move(String sourcePath, String targetPath)
          Move a file.
static boolean putFileInDirectoryIfNot(File file, File folder)
          Put the file into the directory.
static Collection<File> unzip(InputStream source, String destinationFolder)
          Unzip a compressed file.
static void zip(Iterable<File> files, BufferedOutputStream buffer)
          Zip files to a destination stream.
static void zip(Iterable<File> files, File destination)
          Zip files to a destination.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createIfNotExists

public static void createIfNotExists(File file)
Create the file if not exists.

Parameters:
file - The file to create.

getTextOfSystemResource

public static String getTextOfSystemResource(String path)
Return the text of the file.

Parameters:
path - The path to the file.
Returns:
The content of the file

getTextOf

public static String getTextOf(String path)
Return the text of the file referenced by the specified path.

Parameters:
path - The path to the file.
Returns:
The text of the path.

getLinesOf

public static Collection<String> getLinesOf(InputStream stream)
Return the lines of a stream.

Parameters:
stream - The stream to count the lines from.
Returns:
The lines of the stream.

downloadFile

public static void downloadFile(String filePath,
                                String destination)
                         throws FileException
Download a file.

Parameters:
filePath - The path to the file to download.
destination - The path to the destination's file.
Throws:
FileException - If an error occurs during the downloading process

copy

public static void copy(String sourcePath,
                        String targetPath)
                 throws CopyException
Copy a file.

Parameters:
sourcePath - The path of the source.
targetPath - The path of the destination.
Throws:
CopyException - Thrown if an error occurs during the copy.

copy

public static void copy(File source,
                        File target)
                 throws CopyException
This method copy a source's file to a destination's file.

Parameters:
source - The source's file.
target - The destination's file.
Throws:
CopyException - Thrown when a problem occurs during the copy.

move

public static void move(String sourcePath,
                        String targetPath)
                 throws CopyException
Move a file.

Parameters:
sourcePath - The path of the source.
targetPath - The path of the destination.
Throws:
CopyException - Thrown if an error occurs during the move.

unzip

public static Collection<File> unzip(InputStream source,
                                     String destinationFolder)
Unzip a compressed file.

Parameters:
source - The source buffer
destinationFolder - The destination folder.
Returns:
All the files of the zip

zip

public static void zip(Iterable<File> files,
                       File destination)
Zip files to a destination.

Parameters:
files - The files we have to zip.
destination - The archive destination file.

zip

public static void zip(Iterable<File> files,
                       BufferedOutputStream buffer)
Zip files to a destination stream.

Parameters:
files - The files we have to zip.
buffer - The destination stream.

delete

public static void delete(String path)
Delete a file.

Parameters:
path - The path to the file.

delete

public static void delete(File file)
Delete a file.

Parameters:
file - The file.

createEmptyFile

public static void createEmptyFile(String path)
Create an empty file.

Parameters:
path - The path to the empty file.

encryptKey

public static String encryptKey(String key)
Encrypt the string with the SHA-256 Algorithm.

Parameters:
key - The string to encrypt.
Returns:
The encrypted key.

isFileInDirectory

public static boolean isFileInDirectory(File file,
                                        File folder)
Test if a file is in a directory.

Parameters:
file - The file to test.
folder - The folder to test.
Returns:
true if the file is in the directory.

clearFolder

public static void clearFolder(File folder)
Clear the folder. It seems delete all files tags starting with deleted and the file who the start tags refer to.

Parameters:
folder - The folder to clean.

putFileInDirectoryIfNot

public static boolean putFileInDirectoryIfNot(File file,
                                              File folder)
Put the file into the directory.

Parameters:
file - The file.
folder - The folder.
Returns:
true if the file is now in directory.

asInputStream

public static InputStream asInputStream(String path)
                                 throws FileNotFoundException
Return an input stream to the path.

Parameters:
path - The path to get an input stream for.
Returns:
An input stream to the file path.
Throws:
FileNotFoundException - If the path doesn't represent an existing file.

close

public static void close(Closeable closable)
Close a closable resource.

Parameters:
closable - The closable resource to close.

close

public static void close(ZipFile zipFile)
Close a zip file.

Parameters:
zipFile - The zip file to close.

getAnExistingFile

public static File getAnExistingFile()
Return an existing file.

Returns:
An existing file object.

getFileSize

public static long getFileSize(String path)
Return the size of the file specified by the path.

Parameters:
path - The path to the file.
Returns:
The size of the file or 0 if the file doesn't exist.

getFileSize

public static long getFileSize(File file)
Return the size of the specified file.

Parameters:
file - The file to calc the size.
Returns:
The size of the file or 0 if the file doesn't exist.

getLastModifiedDate

public static Date getLastModifiedDate(String path)
Return the date of the last modification of the file denoted by the specified path.

Parameters:
path - The path to the file.
Returns:
The date of the last modification of the file or null if the file doesn't not exists.

getLastModifiedDate

public static Date getLastModifiedDate(File file)
Return the date of the last modification of the specified file.

Parameters:
file - The file.
Returns:
The date of the last modification of the file or null if the file doesn't not exists.

getFreeName

public static String getFreeName(String folder,
                                 String name)
Return the next free name for the specified name in the specified folder. If there is also a file named name in the specified folder, it will search for files name[n].extension while it find a not existing file.

Parameters:
folder - The folder to search free name in.
name - The name to add.
Returns:
The next free name for the specified name in the specified folder.

getFilesOfFolder

public static Collection<File> getFilesOfFolder(File folder,
                                                FileFilter fileFilter)
Return all the files of the folder include the sub files and folders with no level limit.

Parameters:
folder - The folder to get the files from.
fileFilter - The filter to use to select the files.
Returns:
A Collection containing all the files of the folder and his sub folders selected by the specified file filter.


Copyright © 2010 JTheque. All Rights Reserved.