Hilbert II - Version 0.03.09

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.15 $

Method Summary
static void close(InputStream in)
          Closes input stream without exception.
static void close(Reader reader)
          Closes input reader without exception.
static boolean compareFilesBinary(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, boolean deleteDir)
          Delete file directory recursive.
static String getDefaultEncoding()
          Get default encoding for this system.
static Object getFieldContent(Object obj, String name)
          This method returns the contents of an object variable (even if it is private).
static File getStartDirectory(String application)
          Get start directory for application.
static File getUserHomeDirectory()
          Get home directory of user.
static String getWorkingEncoding(String encoding)
          Get working Java encoding.
static boolean isWebStarted()
          Was the application started by Java Webstart?
static void loadFile(File file, StringBuffer buffer)
          Deprecated. Use loadFile(File, StringBuffer, String).
static void loadFile(File file, StringBuffer buffer, String encoding)
          Reads contents of a file into a string buffer.
static String loadFile(String filename, String encoding)
          Reads a file and returns the contents as a String.
static void loadFile(String filename, StringBuffer buffer, String encoding)
          Reads contents of a file into a string buffer.
static void loadFile(URL url, StringBuffer buffer)
          Deprecated. Choose correct encoding.
static void loadFile(URL url, StringBuffer buffer, String encoding)
          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 Properties loadProperties(URL url)
          Loads a property file from given URL.
static void loadReader(Reader in, StringBuffer buffer)
          Reads contents of a Reader into a string buffer.
static void loadStream(InputStream in, StringBuffer buffer)
          Deprecated. Use loadReader(Reader, StringBuffer).
static void printAllSystemProperties()
          Print current system properties to System.out.
static void saveFile(File file, String text)
          Deprecated. Use saveFile(File, String, String) that has an encoding parameter.
static void saveFile(File file, StringBuffer text)
          Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding parameter.
static void saveFile(File file, StringBuffer text, String encoding)
          Saves a String in a file.
static void saveFile(File file, String text, String encoding)
          Saves a String in a file.
static void saveFile(String filename, String text)
          Deprecated. Use saveFile(File, String, String) that has an encoding.
static void saveFile(String filename, StringBuffer text)
          Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding.
static void saveFileBinary(File file, byte[] data)
          Saves a data in a file.
static void saveFileBinary(URL url, File file)
          Save binary contents of an URL into a file.
static void setFieldContent(Object obj, String name, Object value)
          This method sets the contents of an object variable (even if it is private).
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

getDefaultEncoding

public static String getDefaultEncoding()
Get default encoding for this system.

Returns:
Default encoding for this system.

getWorkingEncoding

public static String getWorkingEncoding(String encoding)
Get working Java encoding.

Parameters:
encoding - Try this encoding.
Returns:
This is encoding if it is supported. Or an other encoding that is supported by this system.

loadFile

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

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

loadFile

public static void loadFile(String filename,
                            StringBuffer buffer,
                            String encoding)
                     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.
encoding - Take this encoding.
Throws:
IOException - File exception occurred.

loadStream

public static void loadStream(InputStream in,
                              StringBuffer buffer)
                       throws IOException
Deprecated. Use loadReader(Reader, StringBuffer).

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.

loadReader

public static void loadReader(Reader in,
                              StringBuffer buffer)
                       throws IOException
Reads contents of a Reader into a string buffer.

Parameters:
in - This reader 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
Deprecated. Use loadFile(File, StringBuffer, String).

Reads contents of a file into a string buffer. Uses default encoding.

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

loadFile

public static void loadFile(File file,
                            StringBuffer buffer,
                            String encoding)
                     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.
encoding - Take this encoding.
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
Deprecated. Choose correct encoding.

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.

loadFile

public static void loadFile(URL url,
                            StringBuffer buffer,
                            String encoding)
                     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.
encoding - Take this encoding.
Throws:
IOException - Reading failed.

saveFileBinary

public static void saveFileBinary(URL url,
                                  File file)
                           throws IOException
Save binary contents of an URL into a file.

Parameters:
url - This URL will be loaded.
file - Write into this file.
Throws:
IOException - Reading or writing 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
Deprecated. Use saveFile(File, String, String) that has an encoding.

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
Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding.

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
Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding parameter.

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
Deprecated. Use saveFile(File, String, String) that has an encoding parameter.

Saves a String in a file. Uses default encoding.

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

saveFile

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

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

saveFile

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

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

saveFileBinary

public static void saveFileBinary(File file,
                                  byte[] data)
                           throws IOException
Saves a data 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.

compareFilesBinary

public static boolean compareFilesBinary(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 binary equal?
Throws:
IOException - File exception occurred.

deleteDir

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

Parameters:
directory - Directory to delete.
deleteDir - Delete directory itself too?
Returns:
Was deletion successful?

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.


close

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

Parameters:
in - Input stream, maybe null.

close

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

Parameters:
reader - Reader, maybe null.

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.

isWebStarted

public static final boolean isWebStarted()
Was the application started by Java Webstart?

Returns:
Was the application started by Java Webstart.

loadProperties

public static Properties loadProperties(URL url)
                                 throws IOException
Loads a property file from given URL.

Parameters:
url - URL to load properties from.
Returns:
Loaded properties.
Throws:
MalformedURLException - Invalid URL.
IOException - Reading error.

getFieldContent

public static Object getFieldContent(Object obj,
                                     String name)
This method returns the contents of an object variable (even if it is private).

Parameters:
obj - Object.
name - Variable name
Returns:
Contents of variable.

setFieldContent

public static void setFieldContent(Object obj,
                                   String name,
                                   Object value)
This method sets the contents of an object variable (even if it is private).

Parameters:
obj - Object.
name - Variable name.
value - Value to set.

Hilbert II - Version 0.03.09

©left GNU General Public Licence
All Rights Reserved.