de.jstacs.io
Class FileManager

java.lang.Object
  extended by de.jstacs.io.FileManager

public class FileManager
extends Object

This class is for handling Files. The most important methods of this class are for writing a StringBuffer to or reading it from a File. This is useful for all objects that implement Storable and should be saved in or loaded from a File. Additionally, two write and read methods based on OutputStream and InputStream have been implemented to allow for handling compressed data via, e.g., GZIPOutputStream and GZIPInputStream.

Author:
Jens Keilwagen
See Also:
Storable, File

Method Summary
static int copy(File source, File target, FileFilter filter, boolean newer)
          This method copies all Files and directories, if selected, from a source File, i.e.
static void copy(String from, String to)
          This method copies a File in a faster manner.
static void copy(String from, String to, byte[] buffer)
          This method copies a File in a faster manner using a specified buffer.
static StringBuffer read(Reader reader)
          This method reads a StringBuffer from a given Reader.
static StringBuffer readFile(File file)
          This method reads a StringBuffer from a given File.
static StringBuffer readFile(String fName)
          This method reads a StringBuffer from a file with the given filen name.
static StringBuffer readInputStream(InputStream inputStream)
          This method reads a StringBuffer from a given InputStream.
static void write(Writer writer, CharSequence buffer)
          This method saves a StringBuffer to a given Writer.
static void writeFile(File outputFile, CharSequence buffer)
          This method saves a StringBuffer to a given File.
static void writeFile(String fName, CharSequence buffer)
          This method saves a StringBuffer to a File with user-specified file name.
static void writeOutputStream(OutputStream outStream, CharSequence buffer)
          This method saves a StringBuffer to a given OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copy

public static int copy(File source,
                       File target,
                       FileFilter filter,
                       boolean newer)
                throws IllegalArgumentException,
                       IOException
This method copies all Files and directories, if selected, from a source File, i.e. directory, to a target File, i.e. directory, that are accepted by the f FileFilter filter.

Parameters:
source - the source directory denoted as File
target - the target directory denoted as File
filter - a FileFilter for the Files that enables the user to copy only specific Files
newer - a switch allowing to copy only files from the source directory that are newer than those in the target directory
Returns:
the number of copied Files
Throws:
IllegalArgumentException - if source and target are not directories
IOException - if something went wrong while copying the Files and directories
See Also:
File, FileFilter

copy

public static void copy(String from,
                        String to)
                 throws IOException
This method copies a File in a faster manner.

Parameters:
from - the File name of the original file
to - the File name of the copied file
Throws:
IOException - if something went wrong
See Also:
copy(String, String, byte[])

copy

public static void copy(String from,
                        String to,
                        byte[] buffer)
                 throws IOException
This method copies a File in a faster manner using a specified buffer.

Parameters:
from - the File name of the original file
to - the File name of the copied file
buffer - an array for reading the content of the original File, the size of the array determines how many bytes are read at once
Throws:
IOException - if something went wrong

readFile

public static StringBuffer readFile(String fName)
                             throws IOException
This method reads a StringBuffer from a file with the given filen name.

Parameters:
fName - the name of the file to be read
Returns:
a StringBuffer with the content of the File
Throws:
IOException - if something went wrong with the File
See Also:
writeFile(File, CharSequence), read(Reader), FileReader

readFile

public static StringBuffer readFile(File file)
                             throws IOException
This method reads a StringBuffer from a given File.

Parameters:
file - the File to be read
Returns:
a StringBuffer with the content of the File
Throws:
IOException - if something went wrong with the File
See Also:
writeFile(File, CharSequence), read(Reader), FileReader

readInputStream

public static StringBuffer readInputStream(InputStream inputStream)
                                    throws IOException
This method reads a StringBuffer from a given InputStream.

Parameters:
inputStream - the InputStream to be read
Returns:
a StringBuffer with the content of the InputStream
Throws:
IOException - if something went wrong with the InputStream
See Also:
writeOutputStream(OutputStream, CharSequence), read(Reader), InputStreamReader

read

public static StringBuffer read(Reader reader)
                         throws IOException
This method reads a StringBuffer from a given Reader.

Parameters:
reader - the Reader to be read
Returns:
a StringBuffer with the content of the Reader
Throws:
IOException - if something went wrong with the Reader
See Also:
write(Writer, CharSequence), BufferedReader

writeFile

public static void writeFile(File outputFile,
                             CharSequence buffer)
                      throws IOException
This method saves a StringBuffer to a given File.

Parameters:
outputFile - the File into which the output should be written
buffer - the buffer to be written in the File
Throws:
IOException - if something went wrong with the File
See Also:
readFile(File), write(Writer, CharSequence)

writeFile

public static void writeFile(String fName,
                             CharSequence buffer)
                      throws IOException
This method saves a StringBuffer to a File with user-specified file name.

Parameters:
fName - the name of the file to be written
buffer - the buffer to be written in the File
Throws:
IOException - if something went wrong with the File
See Also:
readFile(File), writeFile(File, CharSequence)

writeOutputStream

public static void writeOutputStream(OutputStream outStream,
                                     CharSequence buffer)
                              throws IOException
This method saves a StringBuffer to a given OutputStream.

Parameters:
outStream - the OutputStream into which the output should be written
buffer - the buffer to be written in the OutputStream
Throws:
IOException - if something went wrong with the OutputStream
See Also:
readInputStream(InputStream), write(Writer, CharSequence), OutputStreamWriter

write

public static void write(Writer writer,
                         CharSequence buffer)
                  throws IOException
This method saves a StringBuffer to a given Writer.

Parameters:
writer - the Writer into which the output should be written
buffer - the buffer to be written in the Writer
Throws:
IOException - if something went wrong with the Writer
See Also:
read(Reader), BufferedWriter