edu.rice.cs.util
Class FileOps

java.lang.Object
  |
  +--edu.rice.cs.util.FileOps

public abstract class FileOps
extends Object

A class to provide some convenient file operations as static methods. It's abstract to prevent (useless) instantiation, though it can be subclassed to provide convenient namespace importation of its methods.

Version:
$Id: FileOps.java,v 1.4 2002/02/08 14:22:22 brianstoler Exp $

Constructor Summary
FileOps()
           
 
Method Summary
static File createTempDirectory(String name)
          Create a new temporary directory.
static File createTempDirectory(String name, File parent)
          Create a new temporary directory.
static boolean deleteDirectory(File dir)
          Delete the given directory including any files and directories it contains.
static String readFileAsString(File file)
          Read the entire contents of a file and return them.
static byte[] readStreamAsBytes(InputStream stream)
          Reads the stream until it reaches EOF, and then returns the read contents as a byte array.
static void writeStringToFile(File file, String text)
          Writes text to the file.
static File writeStringToNewTempFile(String prefix, String suffix, String text)
          Creates a new temporary file and writes the given text to it.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Constructor Detail

FileOps

public FileOps()
Method Detail

readStreamAsBytes

public static byte[] readStreamAsBytes(InputStream stream)
                                throws IOException
Reads the stream until it reaches EOF, and then returns the read contents as a byte array. This call may block, since it will not return until EOF has been reached.
Parameters:
stream - Input stream to read.
Returns:
Byte array consisting of all data read from stream.

readFileAsString

public static String readFileAsString(File file)
                               throws IOException
Read the entire contents of a file and return them.

writeStringToNewTempFile

public static File writeStringToNewTempFile(String prefix,
                                            String suffix,
                                            String text)
                                     throws IOException
Creates a new temporary file and writes the given text to it.
Parameters:
prefix - Beginning part of file name, before unique number
suffix - Ending part of file name, after unique number
text - Text to write to file
Returns:
name of the temporary file that was created

writeStringToFile

public static void writeStringToFile(File file,
                                     String text)
                              throws IOException
Writes text to the file.
Parameters:
file - File to write to
text - Text to write

createTempDirectory

public static File createTempDirectory(String name)
                                throws IOException
Create a new temporary directory.
Parameters:
name - Non-unique portion of the name of the directory to create.
Returns:
File representing the directory that was created.

createTempDirectory

public static File createTempDirectory(String name,
                                       File parent)
                                throws IOException
Create a new temporary directory.
Parameters:
name - Non-unique portion of the name of the directory to create.
parent - Parent directory to contain the new directory
Returns:
File representing the directory that was created.

deleteDirectory

public static boolean deleteDirectory(File dir)
Delete the given directory including any files and directories it contains.
Parameters:
dir - File object representing directory to delete. If, for some reason, this file object is not a directory, it will still be deleted.
Returns:
true if there were no problems in deleting. If it returns false, something failed and the directory contents likely at least partially still exist.