org.seventytwomiles.springframework.io
Class FileUtils

java.lang.Object
  extended by org.seventytwomiles.springframework.io.FileUtils

public class FileUtils
extends Object

FileUtils utility class extracted from the Spring Framework in order to remove the dependency on Spring for this one class. issue 2 (remove unnecessary dependencies)

Author:
Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner, Matthew Hawthorne, Jeremias Maerki, Stephen Colebourne, Ian Springer, Chris Eldredge, Jim Harrington, Niall Pemberton, Sandy McArthur

Constructor Summary
FileUtils()
           
 
Method Summary
static void closeQuietly(InputStream input)
          Unconditionally close an InputStream.
static void copy(InputStream input, Writer output)
          Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.
static void copy(InputStream inputStream, Writer outputStream, String encoding)
          Copy bytes from an InputStream to chars on a Writer using the specified character encoding.
static void copy(Reader input, OutputStream output)
          Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.
static int copy(Reader input, Writer output)
          Copy chars from a Reader to a Writer.
static String readFileToString(File file, String encoding)
           Reads the contents of a file into a String.
static String toString(InputStream input, String encoding)
          Get the contents of an InputStream as a String using the specified character encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

readFileToString

public static String readFileToString(File file,
                                      String encoding)
                               throws IOException

Reads the contents of a file into a String. There is no readFileToString method without encoding parameter because the default encoding can differ between platforms and therefore results in inconsistent results.

Parameters:
file - the file to read
encoding - the encoding to use, null means platform default
Returns:
The file contents or null if read failed.
Throws:
IOException - in case of an I/O error
UnsupportedEncodingException - if the encoding is not supported by the VM

toString

public static String toString(InputStream input,
                              String encoding)
                       throws IOException

Get the contents of an InputStream as a String using the specified character encoding.

Character encoding names can be found at IANA.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

Parameters:
input - the InputStream to read from
encoding - the encoding to use, null means platform default
Returns:
the requested String
Throws:
NullPointerException - if the input is null
IOException - if an I/O error occurs

copy

public static void copy(InputStream input,
                        Writer output)
                 throws IOException

Copy bytes from an InputStream to chars on a Writer using the default character encoding of the platform.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

This method uses InputStreamReader.

Parameters:
input - the InputStream to read from
output - the Writer to write to
Throws:
NullPointerException - if the input or output is null
IOException - if an I/O error occurs
Since:
Commons IO 1.1

copy

public static void copy(InputStream inputStream,
                        Writer outputStream,
                        String encoding)
                 throws IOException

Copy bytes from an InputStream to chars on a Writer using the specified character encoding.

This method buffers the inputStream internally, so there is no need to use a BufferedInputStream.

Character encoding names can be found at IANA.

This method uses InputStreamReader.

Parameters:
inputStream - the InputStream to read from
outputStream - the Writer to write to
encoding - the encoding to use, null means platform default
Throws:
NullPointerException - if the inputStream or outputStream is null
IOException - if an I/O error occurs
Since:
Commons IO 1.1

copy

public static int copy(Reader input,
                       Writer output)
                throws IOException

Copy chars from a Reader to a Writer.

This method buffers the input internally, so there is no need to use a BufferedReader.

Parameters:
input - the Reader to read from
output - the Writer to write to
Returns:
the number of characters copied
Throws:
NullPointerException - if the input or output is null
IOException - if an I/O error occurs
Since:
Commons IO 1.1

copy

public static void copy(Reader input,
                        OutputStream output)
                 throws IOException
Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

This method buffers the input internally, so there is no need to use a BufferedReader.

Due to the implementation of OutputStreamWriter, this method performs a flush.

This method uses OutputStreamWriter.

Parameters:
input - the Reader to read from
output - the OutputStream to write to
Throws:
NullPointerException - if the input or output is null
IOException - if an I/O error occurs
Since:
Commons IO 1.1

closeQuietly

public static void closeQuietly(InputStream input)
Unconditionally close an InputStream.

Equivalent to InputStream.close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
input - the InputStream to close, may be null or already closed


Copyright © 2007-2008. All Rights Reserved.