|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.io.IOUtils
A static class that provides utility constants and methods for I/O. No instances of this class can be created, only the static members should be used.
Field Summary | |
static char |
EXT_SEPARATOR
The character separating the base name from the extension of a file (a dot). |
static String |
KEY_LOCAL_CHARSET
Configuration and context key: the character set to use when reading and writing local files. |
static int |
STANDARD_BLOCK_SIZE
The standard block size recommended for I/O. |
static String |
STANDARD_HTTP_CHARSET
The standard character set defined by the HTTP standard. |
static String |
STANDARD_UNICODE_CHARSET
The standard Unicode-compatible character set. |
Method Summary | |
static File |
createOutFile(File directory,
String localName)
Creates a file for writing output to. |
static File |
createOutFile(File directory,
String localName,
String outExtension)
Creates a file for writing output to. |
static File |
createOutFile(File directory,
String localName,
String outExtension,
MutableInt last)
Creates a file for writing output to. |
static String |
getBaseName(File file)
Returns the base name of a file (the local name without getExtension(File) and preceding dot ). |
static String |
getBaseName(URL url)
Returns the base name of an URL (the local name without getExtension(File) and preceding dot ). |
static File |
getDefaultDirectory()
Returns an default directory that can be given as "parent" when constructing file objects. |
static String |
getExtension(File file)
Returns the extension of a file. |
static String |
getExtension(URL url)
Returns the file extension of an URL. |
static String |
getLocalName(URL url,
boolean acceptDir)
Returns the local name of an URL. |
static Reader |
openReader(File file,
Configuration config)
Opens an reader on a local file. |
static Reader |
openReader(File file,
String charset)
Opens an reader on a local file, using a given charset. |
static Writer |
openWriter(File file,
Configuration config)
Opens an writer on a local file. |
static Writer |
openWriter(File file,
String charset)
Opens an writer on a local file, using a given charset. |
static String |
readToString(Reader reader)
Reads the contents of a reader into a string. |
static int |
readToWriter(Reader reader,
Writer writer)
Reads the contents of a reader into a writer. |
static String[] |
readURIList(CharSequence in)
Converts an URI list as defined in RFC 2483 (MIME type text/uri-list ) into an array of strings. |
static String[] |
readURIList(Reader in)
Converts an URI list as defined in RFC 2483 (MIME type text/uri-list ) into an array of strings. |
static void |
setDefaultDirectory(File directory)
Modifies the default directory that can be given as "parent" when constructing file objects. |
static boolean |
tryToClose(InputStream in)
Convenience method for closing an input stream. |
static boolean |
tryToClose(OutputStream out)
Convenience method for closing an output stream. |
static boolean |
tryToClose(Reader reader)
Convenience method for closing a reader. |
static boolean |
tryToClose(Writer writer)
Convenience method for closing a writer. |
static void |
writeToWriter(CharSequence input,
Writer writer)
Writes the contents of a character sequence to a writer. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String KEY_LOCAL_CHARSET
public static final int STANDARD_BLOCK_SIZE
public static final String STANDARD_HTTP_CHARSET
public static final String STANDARD_UNICODE_CHARSET
public static final char EXT_SEPARATOR
Method Detail |
public static final File createOutFile(File directory, String localName)
createOutFile(File, String, String)
without specifying an
extension.
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extension
public static final File createOutFile(File directory, String localName, String outExtension)
File.mkdir()
method).
The name of the file is derivated from the given local name by
replacing the extension of the input file (if any) by the specified
outExtension
. If a file with this name already exists,
"2" or the next available number is inserted before the extension unless
an unused file name is found.
E.g. for input file "test.txt" and output extension "out", the file "test.out" is created. If this file already exists, the file "test2.out" is created instead (or "test3.out" etc. unless an unsed name is found).
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extensionoutExtension
- the extension to append to the output file
(without a starting dot); ignored if empty or null
public static final File createOutFile(File directory, String localName, String outExtension, MutableInt last)
File.mkdir()
method).
The name of the file is derivated from the given local name by
replacing the extension of the input file (if any) by the specified
outExtension
. If a file with this name already exists,
"2" or the next available number is inserted before the extension unless
an unused file name is found.
E.g. for input file "test.txt" and output extension "out", the file "test.out" is created. If this file already exists, the file "test2.out" is created instead (or "test3.out" etc. unless an unsed name is found).
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extensionoutExtension
- the extension to append to the output file
(without a starting dot); ignored if empty or null
last
- if not null
, assumped to wrap the last numeric
prefix already in use, i.e. the first tried prefix will the the value
of this + 1; after determining a file name, the value will be set to the
numeric prefix used this time
public static String getBaseName(File file)
getExtension(File)
and preceding dot
).
file
- the file to check
public static String getBaseName(URL url)
getExtension(File)
and preceding dot
).
url
- the URL to check
public static File getDefaultDirectory()
null
so the "system-dependent
default directory" (typically the working directory) will be used.
public static String getExtension(File file)
file
- the file to check
public static String getExtension(URL url)
url
- the URL to check
public static String getLocalName(URL url, boolean acceptDir)
For URLs that are recognizable as directories (i.e. end in a slash),
the name of the final directory is returned if acceptDir
is true
-- otherwise the empty string is returned.
url
- the URL to checkacceptDir
- whether to return a final directory name
(see description)
public static Reader openReader(File file, Configuration config) throws FileNotFoundException, UnsupportedEncodingException
configured character set
-- if not specified, the default charset of
the current platform is used instead. Don't forget to finally
close any reader you open!
file
- the file to readconfig
- the configuration to use
FileNotFoundException
- if the file does not exist, is a
directory rather than a regular file, or for some other reason cannot
be opened for reading
UnsupportedEncodingException
- if the named charset is not
supportedpublic static Reader openReader(File file, String charset) throws FileNotFoundException, UnsupportedEncodingException
file
- the file to readcharset
- the character set to use for reading the file;
if null
, the default charset of the current platform is used
FileNotFoundException
- if the file does not exist, is a
directory rather than a regular file, or for some other reason cannot
be opened for reading
UnsupportedEncodingException
- if the named charset is not
supportedpublic static Writer openWriter(File file, Configuration config) throws IOException, UnsupportedEncodingException
configured character set
-- if not specified, the default charset of
the current platform is used instead. Don't forget to finally
close any writer you open!
file
- the file to write toconfig
- the configuration to use
IOException
- if the file is a directory or for some
other reason cannot be opened for writing
UnsupportedEncodingException
- if the named charset is not
supportedpublic static Writer openWriter(File file, String charset) throws IOException, UnsupportedEncodingException
file
- the file to write tocharset
- the character set to use for writing the file;
if null
, the default charset of the current platform is used
IOException
- if the file is a directory or for some
other reason cannot be opened for writing
UnsupportedEncodingException
- if the named charset is not
supportedpublic static String readToString(Reader reader) throws IOException
reader
- the reader to use
IOException
- if an I/O error occurspublic static int readToWriter(Reader reader, Writer writer) throws IOException
reader
- the reader to read fromwriter
- the writer to write to write to
IOException
- if an I/O error occurspublic static String[] readURIList(CharSequence in)
text/uri-list
) into an array of strings. Comment lines
in the input are ignored.
in
- the URI list to convert
public static String[] readURIList(Reader in) throws IOException
text/uri-list
) into an array of strings. Comment lines
in the input are ignored.
in
- a reader containing the URI list to convert
IOException
- if an I/O error occurspublic static void setDefaultDirectory(File directory) throws IllegalArgumentException
null
so the "system-dependent
default directory" (typically the working directory) will be used.
directory
- the new directory to use
IllegalArgumentException
- if the given directory is neither
null
nor an existing directorypublic static boolean tryToClose(InputStream in)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
in
- the input stream to close (might be null
)
true
if the stream was closed successfully,
false
otherwise (the stream was null
or an
exception was thrown during closing)public static boolean tryToClose(OutputStream out)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
out
- the output stream to close (might be null
)
true
if the stream was closed successfully,
false
otherwise (the stream was null
or an
exception was thrown during closing)public static boolean tryToClose(Reader reader)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
reader
- the reader to close (might be null
)
true
if the reader was closed successfully,
false
otherwise (reader was null
or an
exception was thrown during closing)public static boolean tryToClose(Writer writer)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
writer
- the writer to close (might be null
)
true
if the writer was closed successfully,
false
otherwise (writer was null
or an
exception was thrown during closing)public static void writeToWriter(CharSequence input, Writer writer) throws IOException
input
- the text to send to the wrierwriter
- the writer to write to write to
IOException
- if an I/O error occurs
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |