net.sourceforge.xconf.toolbox
Class IOUtils

java.lang.Object
  extended by net.sourceforge.xconf.toolbox.IOUtils

public class IOUtils
extends Object

Common IO utility methods for file/stream reading and writing.

If you are using JDK 1.5 or later, try the generics-enhanced IOUtils version.

Author:
Tom Czarniecki

Constructor Summary
IOUtils()
           
 
Method Summary
static void copy(InputStream src, OutputStream dest)
          Read all bytes from input stream and write them to the output stream.
static byte[] readBytes(File file)
          Read all bytes from the file.
static byte[] readBytes(InputStream input)
          Read all bytes from the input stream.
static byte[] readBytesAndClose(InputStream input)
          Read all bytes from the input stream.
static List readLines(File file)
          Read all lines from the file using default platform end-of-line characters and the default platform character set.
static List readLines(InputStream input)
          Read all lines from the input stream using default platform end-of-line characters and the default platform character set.
static List readLinesAndClose(InputStream input)
          Read all lines from the input stream using default platform end-of-line characters and the default platform character set.
static String readText(File file)
          Read all bytes from the file and return them as a string using the default platform character set.
static String readText(InputStream input)
          Read all bytes from the input stream and return them as a string using the default platform character set.
static String readTextAndClose(InputStream input)
          Read all bytes from the input stream and return them as a string using the default platform character set.
static void writeBytes(File file, byte[] bytes)
          Writes the given byte array to the file.
static void writeLines(File file, List lines)
          Writes the given lines of text to the file using the platform default character set and the platform default end-of-line characters.
static void writeLines(OutputStream out, List lines)
          Writes the given lines of text to the OutputStream using the platform default character set and the platform default end-of-line characters.
static void writeText(File file, String text)
          Writes the given text to the file using the platform default character set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtils

public IOUtils()
Method Detail

copy

public static void copy(InputStream src,
                        OutputStream dest)
                 throws IOException
Read all bytes from input stream and write them to the output stream.

Throws:
IOException

readText

public static String readText(File file)
                       throws IOException
Read all bytes from the file and return them as a string using the default platform character set.

Throws:
IOException

readText

public static String readText(InputStream input)
                       throws IOException
Read all bytes from the input stream and return them as a string using the default platform character set.

NOTE: this method does NOT close the given input stream.

Throws:
IOException
See Also:
readTextAndClose(InputStream)

readTextAndClose

public static String readTextAndClose(InputStream input)
                               throws IOException
Read all bytes from the input stream and return them as a string using the default platform character set.

NOTE: this method also closes the given input stream.

Throws:
IOException
See Also:
readText(InputStream)

readLines

public static List readLines(File file)
                      throws IOException
Read all lines from the file using default platform end-of-line characters and the default platform character set.

Returns:
A list of String objects - one for each line.
Throws:
IOException

readLines

public static List readLines(InputStream input)
                      throws IOException
Read all lines from the input stream using default platform end-of-line characters and the default platform character set.

NOTE: this method does NOT close the given input stream.

Returns:
A list of String objects - one for each line.
Throws:
IOException
See Also:
readLinesAndClose(InputStream)

readLinesAndClose

public static List readLinesAndClose(InputStream input)
                              throws IOException
Read all lines from the input stream using default platform end-of-line characters and the default platform character set.

NOTE: this method closes the given input stream.

Returns:
A list of String objects - one for each line.
Throws:
IOException
See Also:
readLines(InputStream)

readBytes

public static byte[] readBytes(File file)
                        throws IOException
Read all bytes from the file.

Throws:
IOException

readBytes

public static byte[] readBytes(InputStream input)
                        throws IOException
Read all bytes from the input stream.

NOTE: this method does NOT close the given input stream.

Throws:
IOException
See Also:
readBytesAndClose(InputStream)

readBytesAndClose

public static byte[] readBytesAndClose(InputStream input)
                                throws IOException
Read all bytes from the input stream.

NOTE: this method closes the given input stream.

Throws:
IOException
See Also:
readBytesAndClose(InputStream)

writeText

public static void writeText(File file,
                             String text)
                      throws IOException
Writes the given text to the file using the platform default character set.

Throws:
IOException

writeLines

public static void writeLines(File file,
                              List lines)
                       throws IOException
Writes the given lines of text to the file using the platform default character set and the platform default end-of-line characters.

Throws:
IOException

writeLines

public static void writeLines(OutputStream out,
                              List lines)
                       throws IOException
Writes the given lines of text to the OutputStream using the platform default character set and the platform default end-of-line characters. The OutputStream is closed after writing.

Throws:
IOException

writeBytes

public static void writeBytes(File file,
                              byte[] bytes)
                       throws IOException
Writes the given byte array to the file.

Throws:
IOException


Copyright © 2007 Thomas Czarniecki. All Rights Reserved.