org.pf.file
Class FileUtil

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

public class FileUtil
extends 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 String LINE_SEPARATOR
          The lines.separator from the system properties as a constant
 
Method Summary
 boolean close(InputStream stream)
          Close the given stream ignoring any exception.
 boolean close(OutputStream stream)
          Close the given stream ignoring any exception.
 void copyStream(InputStream inStream, OutputStream outStream)
          Copies all data from the iniput stream to the output stream using a buffer with the default size (1024 bytes).
 void copyStream(InputStream inStream, 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(InputStream inStream, StringWriter writer)
           
 void copyText(Reader reader, StringWriter writer)
          Copies all text lines from the specified reader to the given writer.
static FileUtil current()
           
protected  int indexOfPreceedingNotNullElement(String[] elements, int start)
           
 String javaFilename(String filename)
          Returns the given filename in the platform independent way that Java understands.
 void processTextLines(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(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(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.
 String readTextFrom(File file)
          Reads the whole content of the specified file and returns it as a string.
 String readTextFrom(InputStream inStream)
          Reads the whole content of the given input stream and returns it as a string.
 String readTextFrom(String filename)
          Reads the whole content of the file with the given name and returns it as a string.
 String standardize(String filename)
          Convert the filename to a canonical (see java.io.File.getCanonicalPath()) format and replace any backslashes '\' by slashes ('/').
protected  String standardizeFilename(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 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(InputStream inStream,
                       OutputStream outStream)
                throws 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:
IOException

copyStream

public void copyStream(InputStream inStream,
                       OutputStream outStream,
                       int bufSize)
                throws 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:
IOException

readTextFrom

public String readTextFrom(InputStream inStream)
                    throws 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:
IOException

readTextFrom

public String readTextFrom(String filename)
                    throws 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:
IOException

readTextFrom

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

Throws:
IOException

copyText

public void copyText(Reader reader,
                     StringWriter writer)
              throws 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:
IOException

processTextLines

public void processTextLines(String filename,
                             LineProcessor processor)
                      throws 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:
IOException

processTextLines

public void processTextLines(InputStream inStream,
                             LineProcessor processor)
                      throws 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:
IOException

processTextLines

public void processTextLines(Reader reader,
                             LineProcessor processor)
                      throws 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:
IOException

close

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


close

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


standardize

public String standardize(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 String javaFilename(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(InputStream inStream,
                        StringWriter writer)
                 throws IOException
Throws:
IOException

standardizeFilename

protected String standardizeFilename(String filename)

indexOfPreceedingNotNullElement

protected int indexOfPreceedingNotNullElement(String[] elements,
                                              int start)

str

protected StringUtil str()