org.jppf.utils
Class FileUtils

java.lang.Object
  extended by org.jppf.utils.FileUtils

public final class FileUtils
extends Object

This class provides a set of utility methods for reading, writing and manipulating files.

Author:
Laurent Cohen

Method Summary
static void closeInputStream(InputStream is, org.slf4j.Logger log)
          Attempt to close the specified input stream and log any eventual error.
static void copyStream(InputStream is, OutputStream os)
          Copy the data read from the specified input stream to the specified output stream.
static boolean deletePath(File path)
          Delete the specified path, recursively if this is a directory.
static byte[] getFileAsByte(File file)
          Get the content of a file as an array of bytes.
static byte[] getFileAsByte(String path)
          Get the content of a file as an array of bytes.
static String getFileExtension(File file)
          Get the extension of a file.
static String getFileExtension(String filePath)
          Get the extension of a file.
static InputStream getFileInputStream(String path)
          Get an input stream given a file path.
static String getFileName(String filePath)
          Get the name of a file from its full path.
static List<String> getFilePathList(String fileListPath)
          Get a list of files whose paths are found in a text file.
static Reader getFileReader(String path)
          Load a file from the specified path.
static byte[] getInputStreamAsByte(InputStream is)
          Get the content of an input stream as an array of bytes.
static String getParentFolder(String filePath)
          Get the parent folder of a file or directory from its full path.
static void main(String... args)
          Entry point for the splitTextFile() method.
static String readTextFile(File file)
          Read the content of a specified file into a string.
static String readTextFile(Reader aReader)
          Read the content of a specified reader into a string.
static String readTextFile(String filename)
          Read the content of a specified file into a string.
static void splitTextFile(String file, int splitSize)
          Split a file into multiple files whose size is as close as possible to the specified split size.
static List<String> textFileAsLines(Reader aReader)
          Read the content of a specified reader into a string.
static File[] toFiles(File dir, String... names)
          Convert a set of file names into a set of File objects.
static URL[] toURLs(File... files)
          Convert a set of file paths into a set of URLs.
static void writeBytesToFile(byte[] data, File path)
          Write a byte array into an file.
static void writeBytesToFile(byte[] data, String path)
          Write a byte array into an file.
static void writeBytesToStream(byte[] data, OutputStream os)
          Write a byte array into an output stream.
static void writeTextFile(File file, String content)
          Write the content of a string into a specified file.
static void writeTextFile(String filename, String content)
          Write the content of a string into a specified file.
static void writeTextFile(Writer dest, String content)
          Write the content of a string into a specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readTextFile

public static String readTextFile(Reader aReader)
                           throws IOException
Read the content of a specified reader into a string.

Parameters:
aReader - the reader to read the vcontent from.
Returns:
the content of the file as a string.
Throws:
IOException - if the file can't be found or read.

textFileAsLines

public static List<String> textFileAsLines(Reader aReader)
                                    throws IOException
Read the content of a specified reader into a string.

Parameters:
aReader - the reader to read the vcontent from.
Returns:
the content of the file as a string.
Throws:
IOException - if the file can't be found or read.

readTextFile

public static String readTextFile(File file)
                           throws IOException
Read the content of a specified file into a string.

Parameters:
file - the file to read.
Returns:
the content of the file as a string.
Throws:
IOException - if the file can't be found or read.

readTextFile

public static String readTextFile(String filename)
                           throws IOException
Read the content of a specified file into a string.

Parameters:
filename - the location of the file to read.
Returns:
the content of the file as a string.
Throws:
IOException - if the file can't be found or read.

writeTextFile

public static void writeTextFile(String filename,
                                 String content)
                          throws IOException
Write the content of a string into a specified file.

Parameters:
filename - the location of the file to write to.
content - the content to wwrite into the file.
Throws:
IOException - if the file can't be found or read.

writeTextFile

public static void writeTextFile(File file,
                                 String content)
                          throws IOException
Write the content of a string into a specified file.

Parameters:
file - the location of the file to write to.
content - the content to wwrite into the file.
Throws:
IOException - if the file can't be found or read.

writeTextFile

public static void writeTextFile(Writer dest,
                                 String content)
                          throws IOException
Write the content of a string into a specified file.

Parameters:
dest - the file to write to.
content - the content to wwrite into the file.
Throws:
IOException - if the file can't be found or read.

getFileInputStream

public static InputStream getFileInputStream(String path)
                                      throws IOException
Get an input stream given a file path.

Parameters:
path - the path to the file to lookup.
Returns:
a InputStream instance, or null if the file could not be found.
Throws:
IOException - if an IO error occurs while looking up the file.

getFileReader

public static Reader getFileReader(String path)
                            throws IOException
Load a file from the specified path. This method looks up the schema first in the file system, then in the classpath if it is not found in the file system.

Parameters:
path - the path to the file to load.
Returns:
a Reader instance, or null if the schema file could not be found.
Throws:
IOException - if an IO error occurs while looking up the file.

getFilePathList

public static List<String> getFilePathList(String fileListPath)
                                    throws IOException
Get a list of files whose paths are found in a text file.

Parameters:
fileListPath - the path to the file that holds the list of documents to validate.
Returns:
the file paths as a lst of strings.
Throws:
IOException - if an error occurs while looking up or reading the file.

getFileExtension

public static String getFileExtension(String filePath)
Get the extension of a file.

Parameters:
filePath - the file from which to get the extension.
Returns:
the file extension, or null if it si not a file or does not have an extension.

getFileExtension

public static String getFileExtension(File file)
Get the extension of a file.

Parameters:
file - the file from which to get the extension.
Returns:
the file extension, or null if it si not a file or does not have an extension.

getFileName

public static String getFileName(String filePath)
Get the name of a file from its full path.

Parameters:
filePath - the file from which to get the file name.
Returns:
the file name without path information.

getParentFolder

public static String getParentFolder(String filePath)
Get the parent folder of a file or directory from its full path.

Parameters:
filePath - the path from which to get the parent path.
Returns:
the parent folder path.

splitTextFile

public static void splitTextFile(String file,
                                 int splitSize)
                          throws IOException
Split a file into multiple files whose size is as close as possible to the specified split size.

Parameters:
file - the etxt file to split.
splitSize - the maximum number of lines of each resulting file.
Throws:
IOException - if an IO error occurs.

main

public static void main(String... args)
Entry point for the splitTextFile() method.

Parameters:
args - contains the arguments for the splitTextFile() method.

getFileAsByte

public static byte[] getFileAsByte(String path)
                            throws IOException
Get the content of a file as an array of bytes.

Parameters:
path - the path of the file to read from as a string.
Returns:
a byte array.
Throws:
IOException - if an IO error occurs.

getFileAsByte

public static byte[] getFileAsByte(File file)
                            throws IOException
Get the content of a file as an array of bytes.

Parameters:
file - the abstract path of the file to read from.
Returns:
a byte array.
Throws:
IOException - if an IO error occurs.

getInputStreamAsByte

public static byte[] getInputStreamAsByte(InputStream is)
                                   throws IOException
Get the content of an input stream as an array of bytes.

Parameters:
is - the input stream to read from.
Returns:
a byte array.
Throws:
IOException - if an IO error occurs.

copyStream

public static void copyStream(InputStream is,
                              OutputStream os)
                       throws IOException
Copy the data read from the specified input stream to the specified output stream.

Parameters:
is - the input stream to read from.
os - the output stream to write to.
Throws:
IOException - if an I/O error occurs.

toFiles

public static File[] toFiles(File dir,
                             String... names)
Convert a set of file names into a set of File objects.

Parameters:
dir - the directory in which the files are located
names - the name part of each file (not the full path)
Returns:
an array of File objects.

toURLs

public static URL[] toURLs(File... files)
Convert a set of file paths into a set of URLs.

Parameters:
files - the files whose path is to be converted to a URL.
Returns:
an array of URL objects.

writeBytesToStream

public static void writeBytesToStream(byte[] data,
                                      OutputStream os)
                               throws IOException
Write a byte array into an output stream.

Parameters:
data - the byte array to write.
os - the output stream to write to.
Throws:
IOException - if an I/O error occurs.

writeBytesToFile

public static void writeBytesToFile(byte[] data,
                                    String path)
                             throws IOException
Write a byte array into an file.

Parameters:
data - the byte array to write.
path - the path to the file to write to.
Throws:
IOException - if an I/O error occurs.

writeBytesToFile

public static void writeBytesToFile(byte[] data,
                                    File path)
                             throws IOException
Write a byte array into an file.

Parameters:
data - the byte array to write.
path - the path to the file to write to.
Throws:
IOException - if an I/O error occurs.

closeInputStream

public static void closeInputStream(InputStream is,
                                    org.slf4j.Logger log)
Attempt to close the specified input stream and log any eventual error.

Parameters:
is - the input stream to close.
log - the logger to use; if null no logging occurs.

deletePath

public static boolean deletePath(File path)
Delete the specified path, recursively if this is a directory.

Parameters:
path - the path to delete.
Returns:
true if the folder and all contained files and subfolders were deleted, false otherwise.


Copyright © 2005-2010 JPPF Team.