org.pf.file
Class FileUtil

java.lang.Object
  extended by org.pf.file.FileUtil

public class FileUtil
extends java.lang.Object

This class provides helper methods for file and stream handling. It's an add-on to the java.io package. The service is implemented as a singleton, so use the FileUtil.current() method to get the sole instance.

Version:
1.1
Author:
Manfred Duchrow

Field Summary
protected static int DEFAULT_BUFFER_SIZE
           
static java.lang.String LINE_SEPARATOR
          The lines.separator from the system properties as a constant
 
Method Summary
 boolean close(java.io.InputStream stream)
          Close the given stream ignoring any exception.
 boolean close(java.io.OutputStream stream)
          Close the given stream ignoring any exception.
 void copyStream(java.io.InputStream inStream, java.io.OutputStream outStream)
          Copies all data from the iniput stream to the output stream using a buffer with the default size (1024 bytes).
 void copyStream(java.io.InputStream inStream, java.io.OutputStream outStream, int bufSize)
          Copies all data from the iniput stream to the output stream using a buffer of the given size in bytes.
protected  void copyText(java.io.InputStream inStream, java.io.StringWriter writer)
           
 void copyText(java.io.Reader reader, java.io.StringWriter writer)
          Copies all text lines from the specified reader to the given writer.
static FileUtil current()
           
protected  int indexOfPreceedingNotNullElement(java.lang.String[] elements, int start)
           
 java.lang.String javaFilename(java.lang.String filename)
          Returns the given filename in the platform independent way that Java understands.
 void processTextLines(java.io.InputStream inStream, LineProcessor processor)
          Reads all text lines from the specified input stream and passes them one by one to the given line processor.
 void processTextLines(java.io.Reader reader, LineProcessor processor)
          Reads all text lines from the specified reader and passes them one by one to the given line processor.
 void processTextLines(java.lang.String filename, LineProcessor processor)
          Reads all text lines from the file with the specified name and passes them one by one to the given line processor.
 java.lang.String readTextFrom(java.io.File file)
          Reads the whole content of the specified file and returns it as a string.
 java.lang.String readTextFrom(java.io.InputStream inStream)
          Reads the whole content of the given input stream and returns it as a string.
 java.lang.String readTextFrom(java.lang.String filename)
          Reads the whole content of the file with the given name and returns it as a string.
 java.lang.String standardize(java.lang.String filename)
          Convert the filename to a canonical (see java.io.File.getCanonicalPath()) format and replace any backslashes '\' by slashes ('/').
protected  java.lang.String standardizeFilename(java.lang.String filename)
           
protected  StringUtil str()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
The lines.separator from the system properties as a constant


DEFAULT_BUFFER_SIZE

protected static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values
Method Detail

current

public static FileUtil current()

copyStream

public void copyStream(java.io.InputStream inStream,
                       java.io.OutputStream outStream)
                throws java.io.IOException
Copies all data from the iniput stream to the output stream using a buffer with the default size (1024 bytes). After all data is copied both streams will be closed !

Throws:
java.io.IOException

copyStream

public void copyStream(java.io.InputStream inStream,
                       java.io.OutputStream outStream,
                       int bufSize)
                throws java.io.IOException
Copies all data from the iniput stream to the output stream using a buffer of the given size in bytes. After all data is copied both streams will be closed !

Throws:
java.io.IOException

readTextFrom

public java.lang.String readTextFrom(java.io.InputStream inStream)
                              throws java.io.IOException
Reads the whole content of the given input stream and returns it as a string. The stream will be closed after calling this method. Even if an exception occured!

Parameters:
inStream - The input stream to read
Returns:
The text content of the given stream
Throws:
java.io.IOException

readTextFrom

public java.lang.String readTextFrom(java.lang.String filename)
                              throws java.io.IOException
Reads the whole content of the file with the given name and returns it as a string.

Parameters:
filename - The name of the text containing file
Throws:
java.io.IOException

readTextFrom

public java.lang.String readTextFrom(java.io.File file)
                              throws java.io.IOException
Reads the whole content of the specified file and returns it as a string.

Throws:
java.io.IOException

copyText

public void copyText(java.io.Reader reader,
                     java.io.StringWriter writer)
              throws java.io.IOException
Copies all text lines from the specified reader to the given writer. After that the reader will be closed. Even if an exception occurs.

Parameters:
reader - The reader which provides the text to copy
writer - The writer to which the text will be copied
Throws:
java.io.IOException

processTextLines

public void processTextLines(java.lang.String filename,
                             LineProcessor processor)
                      throws java.io.IOException
Reads all text lines from the file with the specified name and passes them one by one to the given line processor. The processing will be terminated, if the end of the text is reached or if the processor returns false.

Parameters:
filename - The name of the text file to read
processor - The processor that receives the lines from the text
Throws:
java.io.IOException

processTextLines

public void processTextLines(java.io.InputStream inStream,
                             LineProcessor processor)
                      throws java.io.IOException
Reads all text lines from the specified input stream and passes them one by one to the given line processor. The processing will be terminated, if the end of the text is reached or if the processor returns false.
The given input stream will be closed after the execution of this method. Even if an exception occured.

Parameters:
inStream - The input stream that contains the text
processor - The processor that receives the lines from the text
Throws:
java.io.IOException

processTextLines

public void processTextLines(java.io.Reader reader,
                             LineProcessor processor)
                      throws java.io.IOException
Reads all text lines from the specified reader and passes them one by one to the given line processor. The processing will be terminated, if the end of the text is reached or if the processor returns false.

Parameters:
reader - The reader that contains a text stream
processor - The processor that receives the lines from the text
Throws:
java.io.IOException

close

public boolean close(java.io.InputStream stream)
Close the given stream ignoring any exception. Returns true, if the stream was closed successfully, false otherwise


close

public boolean close(java.io.OutputStream stream)
Close the given stream ignoring any exception. Returns true, if the stream was closed successfully, false otherwise


standardize

public java.lang.String standardize(java.lang.String filename)
Convert the filename to a canonical (see java.io.File.getCanonicalPath()) format and replace any backslashes '\' by slashes ('/'). If possible all "." and ".." elements in the path are eliminated.

Parameters:
filename - The filename which has to be standardized
Returns:
An absolute filename that uses slashes to separate its elements

javaFilename

public java.lang.String javaFilename(java.lang.String filename)
Returns the given filename in the platform independent way that Java understands. That is all elements are separated by a forward slash rather than back slashes as on Windows systems.

Parameters:
filename - The name to be modified

copyText

protected void copyText(java.io.InputStream inStream,
                        java.io.StringWriter writer)
                 throws java.io.IOException
Throws:
java.io.IOException

standardizeFilename

protected java.lang.String standardizeFilename(java.lang.String filename)

indexOfPreceedingNotNullElement

protected int indexOfPreceedingNotNullElement(java.lang.String[] elements,
                                              int start)

str

protected StringUtil str()