Hilbert II - Version 0.03.04

org.qedeq.kernel.utility
Class IoUtility

java.lang.Object
  extended byorg.qedeq.kernel.utility.IoUtility

public final class IoUtility
extends Object

A collection of useful static methods for input and output. LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5

Version:
$Revision: 1.10 $

Method Summary
static void closeReader(Reader reader)
          Closes input reader without exception.
static void closeStream(InputStream in)
          Closes input stream without exception.
static boolean compareFiles(File from, File with)
          Compare two files.
static void copyFile(File from, File to)
          Copies a file to a different location.
static void createNecessaryDirectories(File file)
          Creates necessary parent directories for a file.
static String createRelativePath(File orgin, File next)
          Create relative address from orgin to next.
static boolean deleteDir(File directory)
          Delete file directory recursive.
static void deleteLineLeadingWhitespace(StringBuffer buffer)
          Search for first line followed by whitespace and delete this string within the whole text.
static String getClassName(Class clazz)
          Get non qualified class name.
static StringBuffer getSpaces(int length)
          Get amount of spaces.
static File getStartDirectory(String application)
          Get start directory for application.
static File getUserHomeDirectory()
          Get home directory of user.
static boolean isLetterDigitString(String text)
          Tests if given String begins with a letter and contains only letters and digits.
static void loadFile(File file, StringBuffer buffer)
          Reads contents of a file into a string buffer.
static String loadFile(String filename)
          Reads a file and returns the contents as a String.
static void loadFile(String filename, StringBuffer buffer)
          Reads contents of a file into a string buffer.
static void loadFile(URL url, StringBuffer buffer)
          Reads contents of an URL into a string buffer.
static byte[] loadFileBinary(File file)
          Reads a file and returns the contents as a String.
static void loadStream(InputStream in, StringBuffer buffer)
          Reads contents of a stream into a string buffer.
static void printAllSystemProperties()
          Print current system properties to System.out.
static String quote(String unquoted)
          Quotes a String.
static void saveFile(File file, String text)
          Saves a String in a file.
static void saveFile(File file, StringBuffer text)
          Saves a StringBuffer in a file.
static void saveFile(String filename, String text)
          Saves a String into a file.
static void saveFile(String filename, StringBuffer text)
          Saves a StringBuffer in a file.
static void saveFileBinary(File file, byte[] data)
          Saves a String in a file.
static Reader stringToReader(String data)
          Convert String into a Reader.
static URL toUrl(File file)
          Convert file in URL.
static void waitln()
          Waits until a '\n' was read from System.in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

loadFile

public static String loadFile(String filename)
                       throws IOException
Reads a file and returns the contents as a String.

Parameters:
filename - Name of the file (could include path).
Returns:
Contents of file.
Throws:
IOException - File exception occurred.

loadFile

public static void loadFile(String filename,
                            StringBuffer buffer)
                     throws IOException
Reads contents of a file into a string buffer.

Parameters:
filename - Name of the file (could include path).
buffer - Buffer to fill with file contents.
Throws:
IOException - File exception occurred.

loadStream

public static void loadStream(InputStream in,
                              StringBuffer buffer)
                       throws IOException
Reads contents of a stream into a string buffer.

Parameters:
in - This stream will be loaded.
buffer - Buffer to fill with file contents.
Throws:
IOException - File exception occurred.

loadFile

public static void loadFile(File file,
                            StringBuffer buffer)
                     throws IOException
Reads contents of a file into a string buffer.

Parameters:
file - This file will be loaded.
buffer - Buffer to fill with file contents.
Throws:
IOException - File exception occurred.

loadFileBinary

public static final byte[] loadFileBinary(File file)
                                   throws IOException
Reads a file and returns the contents as a String.

Parameters:
file - File to load from.
Returns:
Contents of file.
Throws:
IOException - File exception occurred.

loadFile

public static void loadFile(URL url,
                            StringBuffer buffer)
                     throws IOException
Reads contents of an URL into a string buffer. The filling is character set dependent.

Parameters:
url - This URL will be loaded.
buffer - Buffer to fill with file contents.
Throws:
IOException - Reading failed.

stringToReader

public static final Reader stringToReader(String data)
Convert String into a Reader. Bug ID: 4094886

Parameters:
data - Convert this.
Returns:
Resulting reader.

saveFile

public static void saveFile(String filename,
                            String text)
                     throws IOException
Saves a String into a file.

Parameters:
filename - Name of the file (could include path).
text - Data to save in the file.
Throws:
IOException - File exception occurred.

saveFile

public static void saveFile(String filename,
                            StringBuffer text)
                     throws IOException
Saves a StringBuffer in a file.

Parameters:
filename - Name of the file (could include path).
text - Data to save in the file.
Throws:
IOException - File exception occurred.

saveFile

public static void saveFile(File file,
                            StringBuffer text)
                     throws IOException
Saves a StringBuffer in a file.

Parameters:
file - File to save into.
text - Data to save in the file.
Throws:
IOException - File exception occurred.

saveFile

public static void saveFile(File file,
                            String text)
                     throws IOException
Saves a String in a file.

Parameters:
file - File to save the data in.
text - Data to save in the file.
Throws:
IOException - File exception occurred.

saveFileBinary

public static void saveFileBinary(File file,
                                  byte[] data)
                           throws IOException
Saves a String in a file.

Parameters:
file - File to save the data in.
data - Data to save in the file.
Throws:
IOException - File exception occurred.

copyFile

public static void copyFile(File from,
                            File to)
                     throws IOException
Copies a file to a different location.

Parameters:
from - Copy source.
to - Copy destination.
Throws:
IOException - File exception occurred.

compareFiles

public static boolean compareFiles(File from,
                                   File with)
                            throws IOException
Compare two files.

Parameters:
from - Compare source.
with - Compare with this file.
Returns:
Is the contents of the two files equal?
Throws:
IOException - File exception occurred.

quote

public static String quote(String unquoted)
Quotes a String. If no quotes exist in the String, a quote character is appended at the beginning and the end of the String.

Parameters:
unquoted - the unquoted String
Returns:
quoted String
Throws:
NullPointerException - if unquoted == null

isLetterDigitString

public static boolean isLetterDigitString(String text)
Tests if given String begins with a letter and contains only letters and digits.

Parameters:
text - test this
Returns:
is text only made of letters and digits and has a leading letter?
Throws:
NullPointerException - if text == null

deleteDir

public static boolean deleteDir(File directory)
Delete file directory recursive.

Parameters:
directory - Directory to delete.
Returns:
Was deletion successful?

getSpaces

public static StringBuffer getSpaces(int length)
Get amount of spaces.

Parameters:
length - number of spaces
Returns:
String contains exactly number spaces

getClassName

public static String getClassName(Class clazz)
Get non qualified class name.

Parameters:
clazz - Class.
Returns:
Non qualified class name.

printAllSystemProperties

public static void printAllSystemProperties()
Print current system properties to System.out.


getUserHomeDirectory

public static File getUserHomeDirectory()
Get home directory of user.

Returns:
Home directory of user.

toUrl

public static URL toUrl(File file)
Convert file in URL.

Parameters:
file - File.
Returns:
URL.

createNecessaryDirectories

public static void createNecessaryDirectories(File file)
Creates necessary parent directories for a file.

Parameters:
file - File.

createRelativePath

public static final String createRelativePath(File orgin,
                                              File next)
Create relative address from orgin to next.

Parameters:
orgin - this is the original location
next - this should be the next location
Returns:
relative (or if necessary absolute) file path

waitln

public static void waitln()
Waits until a '\n' was read from System.in.


closeStream

public static void closeStream(InputStream in)
Closes input stream without exception.

Parameters:
in - Input stream, maybe null.

closeReader

public static void closeReader(Reader reader)
Closes input reader without exception.

Parameters:
reader - Reader, maybe null.

deleteLineLeadingWhitespace

public static void deleteLineLeadingWhitespace(StringBuffer buffer)
Search for first line followed by whitespace and delete this string within the whole text.

For example the following text

       Do you know the muffin man,
       The muffin man, the muffin man,
       Do you know the muffin man,
       Who lives on Drury Lane?
will be converted into:
Do you know the muffin man,
The muffin man, the muffin man,
Do you know the muffin man,
Who lives on Drury Lane?

Parameters:
buffer - Work on this text.

getStartDirectory

public static final File getStartDirectory(String application)
Get start directory for application. If this is no Java Webstart version the result is new File(".").

Parameters:
application - Application name, used for Java Webstart version. Should be written in lowercase letters. A "." is automatically appended at the beginning.
Returns:
Start directory for application.

Hilbert II - Version 0.03.04

©left GNU General Public Licence
All Rights Reserved.