org.cert.cc.util
Class IOUtils

java.lang.Object
  extended by org.cert.cc.util.IOUtils

public final class IOUtils
extends Object

IOUtils includes helper methods. All methods are thread safe, and most are null safe.

© 2009 Carnegie Mellon University

Author:
Steve Scholnick

Field Summary
static String NEW_LINE
           
 
Method Summary
static boolean canRead(String path)
          Returns if a file can be read.
static boolean canWrite(String path)
          Returns if a file can be written to.
static void close(Closeable c)
          Closes an Closeable object quietly.
static void delete(String path)
          Deletes an existing file.
static boolean exists(String path)
          Returns if a file already exists.
static byte[] getRawData(InputStream is)
          Extract all of the data from the InputStream is and returns the data.
static File mkdir(String parent, String child)
          Makes a new directory.
static String read(InputStream is)
          Reads in data from is and returns a String representation of the data.
static String read(Reader reader)
          Reads in data provided by the reader and returns a String representation of the data.
static String read(String path)
          Reads in data from the path and returns a String representation of the data.
static void write(File file, String contents)
          Writes out the contents to the path specified.
static void write(String filePath, String contents)
          Writes out the contents to the path specified.
static void write(Writer writer, String contents)
          Writes out the contents to the Writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEW_LINE

public static final String NEW_LINE
Method Detail

getRawData

public static byte[] getRawData(InputStream is)
                         throws IOException
Extract all of the data from the InputStream is and returns the data. The InputStream is will be left open.

Parameters:
is - InputStream to be harvested
Returns:
All of InputStream's data
Throws:
IOException - If unable to retrieve the data

read

public static String read(InputStream is)
                   throws IOException
Reads in data from is and returns a String representation of the data. Should only be used for textual data.

Not null safe

Parameters:
is - InputStream
Returns:
String representation of the is's data
Throws:
IOException - If unable to read the data

read

public static String read(String path)
                   throws IOException
Reads in data from the path and returns a String representation of the data. Should only be used for textual data.

Not null safe

Parameters:
path - Path to a text file
Returns:
textual data
Throws:
IOException - If unable to read the data

read

public static String read(Reader reader)
                   throws IOException
Reads in data provided by the reader and returns a String representation of the data. Should only be used for textual data. The reader is closed when the reading is completed.

The data is read into memory, so it is possible to overload the JVM's memory

Not null safe

Parameters:
reader - Reader
Returns:
textual data
Throws:
IOException - If unable to read the data

write

public static void write(File file,
                         String contents)
                  throws IOException
Writes out the contents to the path specified.

Parameters:
file - File object
contents - contents to be written
Throws:
IOException - If unable to write the contents

write

public static void write(String filePath,
                         String contents)
                  throws IOException
Writes out the contents to the path specified.

Parameters:
filePath - file path
contents - contents to be written
Throws:
IOException - If unable to write the contents

write

public static void write(Writer writer,
                         String contents)
                  throws IOException
Writes out the contents to the Writer. The Writer is closed upon the completion of the writing

Parameters:
writer - Writer
contents - contents to be written
Throws:
IOException - If unable to write the contents

close

public static void close(Closeable c)
Closes an Closeable object quietly.

Parameters:
c - Closeable object

exists

public static boolean exists(String path)
Returns if a file already exists.

Parameters:
path - path to the file
Returns:
boolean

mkdir

public static File mkdir(String parent,
                         String child)
                  throws IOException
Makes a new directory. Returns a File object to the created directory. Unlike File.mkdir():

Parameters:
parent - Parent directory
child - Child directory to be created under the parent
Returns:
The created directory as a File object.
Throws:
IOException - If unable to create the new directory.
See Also:
File.mkdir()

canRead

public static boolean canRead(String path)
Returns if a file can be read.

Parameters:
path - path to the file
Returns:
boolean

canWrite

public static boolean canWrite(String path)
Returns if a file can be written to.

Parameters:
path - path to the file
Returns:
boolean

delete

public static void delete(String path)
                   throws IOException
Deletes an existing file. A non-existing file is simply ignored.

Parameters:
path - path to the file
Throws:
IOException - If unable to delete an existing file


Copyright © 2012 Carnegie Mellon University