public class FileManager extends Object
File
s. 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
.Modifier and Type | Method and Description |
---|---|
static int |
copy(File source,
File target,
FileFilter filter,
boolean newer)
|
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 . |
public static int copy(File source, File target, FileFilter filter, boolean newer) throws IllegalArgumentException, IOException
File
s 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
.source
- the source directory denoted as File
target
- the target directory denoted as File
filter
- a FileFilter
for the File
s that enables the
user to copy only specific File
snewer
- a switch allowing to copy only files from the source directory that are newer than those in the target directoryFile
sIllegalArgumentException
- if source
and target
are not
directoriesIOException
- if something went wrong while copying the File
s and
directoriesFile
,
FileFilter
public static void copy(String from, String to) throws IOException
File
in a faster manner.from
- the File
name of the original fileto
- the File
name of the copied fileIOException
- if something went wrongcopy(String, String, byte[])
public static void copy(String from, String to, byte[] buffer) throws IOException
File
in a faster manner using a specified
buffer.from
- the File
name of the original fileto
- the File
name of the copied filebuffer
- an array for reading the content of the original File
,
the size of the array determines how many byte
s
are read at onceIOException
- if something went wrongpublic static StringBuffer readFile(String fName) throws IOException
StringBuffer
from a file with the given filen name.fName
- the name of the file to be readStringBuffer
with the content of the File
IOException
- if something went wrong with the File
writeFile(File, CharSequence)
,
read(Reader)
,
FileReader
public static StringBuffer readFile(File file) throws IOException
StringBuffer
from a given File
.file
- the File
to be readStringBuffer
with the content of the File
IOException
- if something went wrong with the File
writeFile(File, CharSequence)
,
read(Reader)
,
FileReader
public static StringBuffer readInputStream(InputStream inputStream) throws IOException
StringBuffer
from a given InputStream
.inputStream
- the InputStream
to be readStringBuffer
with the content of the InputStream
IOException
- if something went wrong with the InputStream
writeOutputStream(OutputStream, CharSequence)
,
read(Reader)
,
InputStreamReader
public static StringBuffer read(Reader reader) throws IOException
StringBuffer
from a given Reader
.reader
- the Reader
to be readStringBuffer
with the content of the Reader
IOException
- if something went wrong with the Reader
write(Writer, CharSequence)
,
BufferedReader
public static void writeFile(File outputFile, CharSequence buffer) throws IOException
StringBuffer
to a given File
.outputFile
- the File
into which the output should be writtenbuffer
- the buffer to be written in the File
IOException
- if something went wrong with the File
readFile(File)
,
write(Writer, CharSequence)
public static void writeFile(String fName, CharSequence buffer) throws IOException
StringBuffer
to a File
with user-specified file name.fName
- the name of the file to be writtenbuffer
- the buffer to be written in the File
IOException
- if something went wrong with the File
readFile(File)
,
writeFile(File, CharSequence)
public static void writeOutputStream(OutputStream outStream, CharSequence buffer) throws IOException
StringBuffer
to a given OutputStream
.outStream
- the OutputStream
into which the output should be writtenbuffer
- the buffer to be written in the OutputStream
IOException
- if something went wrong with the OutputStream
readInputStream(InputStream)
,
write(Writer, CharSequence)
,
OutputStreamWriter
public static void write(Writer writer, CharSequence buffer) throws IOException
StringBuffer
to a given Writer
.writer
- the Writer
into which the output should be writtenbuffer
- the buffer to be written in the Writer
IOException
- if something went wrong with the Writer
read(Reader)
,
BufferedWriter