org.qedeq.base.io
Class IoUtility

java.lang.Object
  extended by org.qedeq.base.io.IoUtility

public final class IoUtility
extends java.lang.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

Author:
Michael Meyling

Method Summary
static void close(java.io.InputStream in)
          Closes input stream without exception.
static void close(java.io.OutputStream out)
          Closes out stream without exception.
static void close(java.io.Reader reader)
          Closes input reader without exception.
static void close(java.io.Writer writer)
          Closes writer without exception.
static boolean compareFilesBinary(java.io.File from, java.io.File with)
          Compare two files binary.
static boolean compareTextFiles(java.io.File from, java.io.File with, int startAtLine, java.lang.String encoding)
          Compare two text files.
static boolean compareTextFiles(java.io.File from, java.io.File with, java.lang.String encoding)
          Compare two text files.
static void copy(java.io.File sourceLocation, java.io.File targetLocation)
          Copy one directory to another location.
static void copy(java.lang.String sourceLocation, java.lang.String targetLocation)
          Copy one file or directory to another location.
static void copyFile(java.io.File from, java.io.File to)
          Copies a file to a different location.
static void createNecessaryDirectories(java.io.File file)
          Creates necessary parent directories for a file.
static java.lang.String createRelativePath(java.io.File origin, java.io.File next)
          Create relative address from origin to next.
static boolean deleteDir(java.io.File directory, boolean deleteDir)
          Delete file directory recursive.
static boolean deleteDir(java.io.File directory, java.io.FileFilter filter)
          Delete directory contents for all files that match the filter.
static java.lang.String getDefaultEncoding()
          Get default encoding for this system.
static int[] getJavaVersion()
          Get currently running java version and subversion numbers.
static java.lang.String[][] getSortedSystemProperties()
          Get key sorted list of all System Properties.
static java.io.File getStartDirectory(java.lang.String application)
          Get start directory for application.
static java.io.File getUserHomeDirectory()
          Get home directory of user.
static java.lang.String getWorkingEncoding(java.lang.String encoding)
          Get working Java encoding.
static boolean isSymbolicLink(java.io.File file)
          Determines whether the specified file is a symbolic link rather than an actual file.
static boolean isWebStarted()
          Was the application started by Java Webstart?
static java.util.List listFilesRecursively(java.io.File sourceLocation, java.io.FileFilter filter)
          List all matching files.
static void loadFile(java.io.File file, java.lang.StringBuffer buffer)
          Deprecated. Use loadFile(File, StringBuffer, String).
static void loadFile(java.io.File file, java.lang.StringBuffer buffer, java.lang.String encoding)
          Reads contents of a file into a string buffer.
static java.lang.String loadFile(java.lang.String filename, java.lang.String encoding)
          Reads a file and returns the contents as a String.
static void loadFile(java.lang.String filename, java.lang.StringBuffer buffer, java.lang.String encoding)
          Reads contents of a file into a string buffer.
static void loadFile(java.net.URL url, java.lang.StringBuffer buffer)
          Deprecated. Choose correct encoding.
static void loadFile(java.net.URL url, java.lang.StringBuffer buffer, java.lang.String encoding)
          Reads contents of an URL into a StringBuffer.
static byte[] loadFileBinary(java.io.File file)
          Reads a file and returns the contents as a String.
static java.util.Properties loadProperties(java.net.URL url)
          Loads a property file from given URL.
static void loadReader(java.io.Reader in, java.lang.StringBuffer buffer)
          Reads contents of a Reader into a string buffer.
static void loadStream(java.io.InputStream in, java.lang.StringBuffer buffer)
          Deprecated. Use loadReader(Reader, StringBuffer).
static java.lang.String loadStreamWithoutException(java.io.InputStream in, int maxLength)
          Returns contents of a stream into a string, respecting a maximum length.
static void printAllSystemProperties()
          Print current system properties to System.out.
static void saveFile(java.io.File file, java.lang.String text)
          Deprecated. Use saveFile(File, String, String) that has an encoding parameter.
static void saveFile(java.io.File file, java.lang.StringBuffer text)
          Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding parameter.
static void saveFile(java.io.File file, java.lang.StringBuffer text, java.lang.String encoding)
          Saves a String in a file.
static void saveFile(java.io.File file, java.lang.String text, java.lang.String encoding)
          Saves a String in a file.
static void saveFile(java.io.InputStream in, java.io.File file)
          Save binary contents of an input stream into a file.
static void saveFile(java.lang.String filename, java.lang.String text)
          Deprecated. Use saveFile(File, String, String) that has an encoding.
static void saveFile(java.lang.String filename, java.lang.StringBuffer text)
          Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding.
static void saveFile(java.net.URL url, java.io.File file)
          Save binary contents of an URL into a file.
static void saveFileBinary(java.io.File file, byte[] data)
          Saves a data in a file.
static void sleep(int ms)
          Sleep my little class.
static java.io.Reader stringToReader(java.lang.String data)
          Convert String into a Reader.
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 java.lang.String getDefaultEncoding()
Get default encoding for this system.

Returns:
Default encoding for this system.

getWorkingEncoding

public static java.lang.String getWorkingEncoding(java.lang.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 java.lang.String loadFile(java.lang.String filename,
                                        java.lang.String encoding)
                                 throws java.io.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:
java.io.IOException - File exception occurred.

loadFile

public static void loadFile(java.lang.String filename,
                            java.lang.StringBuffer buffer,
                            java.lang.String encoding)
                     throws java.io.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:
java.io.IOException - File exception occurred.

loadStream

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

Reads contents of a stream into a string buffer. Stream is not closed.

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

loadStreamWithoutException

public static java.lang.String loadStreamWithoutException(java.io.InputStream in,
                                                          int maxLength)
Returns contents of a stream into a string, respecting a maximum length. No exceptions are thrown. Stream is not closed.

Parameters:
in - This stream will be loaded.
maxLength - This length is not exceeded.
Returns:
readData Data read, is not null.

loadReader

public static void loadReader(java.io.Reader in,
                              java.lang.StringBuffer buffer)
                       throws java.io.IOException
Reads contents of a Reader into a string buffer. Reader is not closed.

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

loadFile

public static void loadFile(java.io.File file,
                            java.lang.StringBuffer buffer)
                     throws java.io.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:
java.io.IOException - File exception occurred.

loadFile

public static void loadFile(java.io.File file,
                            java.lang.StringBuffer buffer,
                            java.lang.String encoding)
                     throws java.io.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:
java.io.IOException - File exception occurred.

loadFileBinary

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

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

loadFile

public static void loadFile(java.net.URL url,
                            java.lang.StringBuffer buffer)
                     throws java.io.IOException
Deprecated. Choose correct encoding.

Reads contents of an URL into a string buffer. The filling is character set dependent. Content is added to the end of buffer. (Existing data is not cleared.)

All parameters should not be null.

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

loadFile

public static void loadFile(java.net.URL url,
                            java.lang.StringBuffer buffer,
                            java.lang.String encoding)
                     throws java.io.IOException
Reads contents of an URL into a StringBuffer. The filling is character set dependent. The buffer is not cleared, contents is just added.

All parameters should not be null.

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

saveFile

public static void saveFile(java.net.URL url,
                            java.io.File file)
                     throws java.io.IOException
Save binary contents of an URL into a file. Existing files are overwritten.

Parameters:
url - This URL will be loaded.
file - Write into this file.
Throws:
java.io.IOException - Reading or writing failed.

saveFile

public static void saveFile(java.io.InputStream in,
                            java.io.File file)
                     throws java.io.IOException
Save binary contents of an input stream into a file. The input stream is closed even if exceptions occur. Existing files are overwritten.

Parameters:
in - Read this stream.
file - Write into this file.
Throws:
java.io.IOException - Reading or writing failed.

stringToReader

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

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

saveFile

public static void saveFile(java.lang.String filename,
                            java.lang.String text)
                     throws java.io.IOException
Deprecated. Use saveFile(File, String, String) that has an encoding.

Saves a String into a file. Existing files are overwritten.

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

saveFile

public static void saveFile(java.lang.String filename,
                            java.lang.StringBuffer text)
                     throws java.io.IOException
Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding.

Saves a StringBuffer in a file. Existing files are overwritten.

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

saveFile

public static void saveFile(java.io.File file,
                            java.lang.StringBuffer text)
                     throws java.io.IOException
Deprecated. Use saveFile(File, StringBuffer, String) that has an encoding parameter.

Saves a StringBuffer in a file. Existing files are overwritten.

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

saveFile

public static void saveFile(java.io.File file,
                            java.lang.String text)
                     throws java.io.IOException
Deprecated. Use saveFile(File, String, String) that has an encoding parameter.

Saves a String in a file. Uses default encoding. Existing files are overwritten.

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

saveFile

public static void saveFile(java.io.File file,
                            java.lang.StringBuffer text,
                            java.lang.String encoding)
                     throws java.io.IOException
Saves a String in a file. Existing files are overwritten.

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

saveFile

public static void saveFile(java.io.File file,
                            java.lang.String text,
                            java.lang.String encoding)
                     throws java.io.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:
java.io.IOException - File exception occurred.

saveFileBinary

public static void saveFileBinary(java.io.File file,
                                  byte[] data)
                           throws java.io.IOException
Saves a data in a file. Existing files are overwritten.

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

copyFile

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

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

copy

public static void copy(java.lang.String sourceLocation,
                        java.lang.String targetLocation)
                 throws java.io.IOException
Copy one file or directory to another location. If targetLocation does not exist, it will be created.

Parameters:
sourceLocation - Copy from here. This can be a file or a directory.
targetLocation - Copy to this location. If source is a file this must be a file too.
Throws:
java.io.IOException - Something went wrong.

copy

public static void copy(java.io.File sourceLocation,
                        java.io.File targetLocation)
                 throws java.io.IOException
Copy one directory to another location. If targetLocation does not exist, it will be created.

Parameters:
sourceLocation - Copy from here.
targetLocation - Copy to this location
Throws:
java.io.IOException - Something went wrong.

listFilesRecursively

public static java.util.List listFilesRecursively(java.io.File sourceLocation,
                                                  java.io.FileFilter filter)
                                           throws java.io.IOException
List all matching files. Searches all matching sub directories recursively. Remember to return true for accept(File pathname) if pathname is a directory if you want to search all sub directories! If sourceLocation is a single file, this is the only file that will be in the resulting list.

Parameters:
sourceLocation - Check all files in this directory. (Or add this single file.)
filter - Accept only these directories and files.
Returns:
List of matching files. Contains no directories.
Throws:
java.io.IOException - Something went wrong.

compareFilesBinary

public static boolean compareFilesBinary(java.io.File from,
                                         java.io.File with)
                                  throws java.io.IOException
Compare two files binary.

Parameters:
from - Compare source. This file must be null or be an existing file.
with - Compare with this file. This file must be null or be an existing file.
Returns:
Is the contents of the two files binary equal?
Throws:
java.io.IOException - File exception occurred.

compareTextFiles

public static boolean compareTextFiles(java.io.File from,
                                       java.io.File with,
                                       java.lang.String encoding)
                                throws java.io.IOException
Compare two text files. Ignores different line separators. As there are: LF, CR, CR + LF, NEL, FF, LS, PS.

Parameters:
from - Compare source.
with - Compare with this file.
encoding - Use this character encoding. Must not be null.
Returns:
Is the contents of the two text files equal?
Throws:
java.io.IOException - File exception occurred or encoding is not supported.
java.lang.NullPointerException - Is encoding different from null?

compareTextFiles

public static boolean compareTextFiles(java.io.File from,
                                       java.io.File with,
                                       int startAtLine,
                                       java.lang.String encoding)
                                throws java.io.IOException
Compare two text files. Ignores different line separators. As there are: LF, CR, CR + LF

Parameters:
from - Compare source.
with - Compare with this file.
startAtLine - Start comparing at this line (beginning with 0).
encoding - Use this character encoding. Must not be null.
Returns:
Is the contents of the two text files equal?
Throws:
java.io.IOException - File exception occurred or encoding is not supported.
java.lang.NullPointerException - Is encoding different from null?

deleteDir

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

Parameters:
directory - Directory to delete. Must not be a symbolic link.
deleteDir - Delete directory itself too?
Returns:
Was deletion successful?

deleteDir

public static boolean deleteDir(java.io.File directory,
                                java.io.FileFilter filter)
Delete directory contents for all files that match the filter. The main directory itself is not deleted.

Parameters:
directory - Directory to scan for files to delete.
filter - Filter files (and directories) to delete.
Returns:
Was deletion successful?

isSymbolicLink

public static boolean isSymbolicLink(java.io.File file)
                              throws java.io.IOException
Determines whether the specified file is a symbolic link rather than an actual file. See https://svn.apache.org/repos/asf/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java.

Parameters:
file - File to check.
Returns:
Is the file is a symbolic link?
Throws:
java.io.IOException - IO error while checking the file.

printAllSystemProperties

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


getUserHomeDirectory

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

Returns:
Home directory of user.

createNecessaryDirectories

public static void createNecessaryDirectories(java.io.File file)
                                       throws java.io.IOException
Creates necessary parent directories for a file.

Parameters:
file - File.
Throws:
java.io.IOException - Creation failed.

createRelativePath

public static final java.lang.String createRelativePath(java.io.File origin,
                                                        java.io.File next)
Create relative address from origin to next. The resulting file path has "/" as directory name separator. If the resulting file path is the same as origin specifies, we return "". Otherwise the result will always have an "/" as last character.

Parameters:
origin - This is the original location. Must be a directory.
next - This should be the next location. Must also be a directory.
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(java.io.InputStream in)
Closes input stream without exception.

Parameters:
in - Input stream, maybe null.

close

public static void close(java.io.Writer writer)
Closes writer without exception.

Parameters:
writer - Writer, maybe null.

close

public static void close(java.io.OutputStream out)
Closes out stream without exception.

Parameters:
out - Output stream, maybe null.

close

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

Parameters:
reader - Reader, maybe null.

getStartDirectory

public static final java.io.File getStartDirectory(java.lang.String application)
Get start directory for application. Within the start directory all newly created data is stored in. If this is no Java Webstart version the result is new File("."). Otherwise the start directory is the subdirectory "." concatenated application within user.home. If a system property application + ".startDirectory" is defined we take this as the start directory.

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 java.util.Properties loadProperties(java.net.URL url)
                                           throws java.io.IOException
Loads a property file from given URL.

Parameters:
url - URL to load properties from. Must not be null.
Returns:
Loaded properties.
Throws:
java.io.IOException - Reading error.

sleep

public static void sleep(int ms)
Sleep my little class.

Parameters:
ms - Milliseconds to wait.

getJavaVersion

public static int[] getJavaVersion()
Get currently running java version and subversion numbers. This is the running JRE version. If no version could be identified null is returned.

Returns:
Array of version and subversion numbers.

getSortedSystemProperties

public static java.lang.String[][] getSortedSystemProperties()
Get key sorted list of all System Properties.

Returns:
Array with the two columns key and value.


Copyright © 2014. All Rights Reserved.