org.simoes.util
Class FileUtil

java.lang.Object
  extended byorg.simoes.util.FileUtil

public class FileUtil
extends java.lang.Object

Utility class for manipulating Files in Java that I have found useful.

Author:
Chris Simoes

Constructor Summary
FileUtil()
           
 
Method Summary
static void deleteFile(java.io.File filename)
          Deletes the file passed in.
static void deleteFile(java.lang.String filename)
          Deletes the file passed in.
static void deleteFiles(java.lang.String[] filenames)
          Deletes the files passed in the String array.
static java.io.ByteArrayOutputStream inputStream2ByteArray(java.io.InputStream is)
          Reads the InputStream into the ByteArrayOutputStream.
static java.lang.String inputStream2String(java.io.InputStream is)
          Converts the InputStream to the returned String.
static void readInputStream(java.io.InputStream is, java.io.ByteArrayOutputStream baos)
          Reads the InputStream into the ByteArrayOutputStream.
static int runProgram(java.lang.String program, java.io.ByteArrayOutputStream baos)
          Runs the program passed in.
static java.io.File writeFile(byte[] data, java.lang.String filename)
          Writes the byte[] data to a file named filename.
static java.io.File writeFile(java.io.InputStream inputStream, java.lang.String filename)
          Writes the entire contents of the InputStream to a file named the filename passed in.
static java.io.File writeFile(java.lang.String data, java.lang.String filename)
          Writes the String data to a file named filename.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

writeFile

public static java.io.File writeFile(java.io.InputStream inputStream,
                                     java.lang.String filename)
                              throws java.io.IOException
Writes the entire contents of the InputStream to a file named the filename passed in.

Parameters:
inputStream - - the inputStream to be written to file
filename - - the file that the inputStream will be written to
Returns:
File - a File object representing the file we just created, or null if there was an error.
Throws:
java.io.IOException

writeFile

public static java.io.File writeFile(java.lang.String data,
                                     java.lang.String filename)
                              throws java.io.IOException
Writes the String data to a file named filename.

Parameters:
data - - A String that will be written to file
filename - - the file that the inputStream will be written to
Returns:
File - a File object representing the file we just created, or null if there was an error.
Throws:
java.io.IOException

writeFile

public static java.io.File writeFile(byte[] data,
                                     java.lang.String filename)
                              throws java.io.IOException
Writes the byte[] data to a file named filename.

Parameters:
data - - A byte[] that will be written to file
filename - - the file that the inputStream will be written to
Returns:
File - a File object representing the file we just created, or null if there was an error.
Throws:
java.io.IOException

deleteFiles

public static void deleteFiles(java.lang.String[] filenames)
Deletes the files passed in the String array. Basically it calls the delete method in the File object.

Parameters:
filenames - - the files to be deleted

deleteFile

public static void deleteFile(java.lang.String filename)
Deletes the file passed in. Basically it calls the delete method in the File object.

Parameters:
filename - - the file to be deleted

deleteFile

public static void deleteFile(java.io.File filename)
Deletes the file passed in. Basically it calls the delete method in the File object.

Parameters:
filename - - the file to be deleted

runProgram

public static int runProgram(java.lang.String program,
                             java.io.ByteArrayOutputStream baos)
Runs the program passed in. It the returns the "return code" of the program. 0 usually means normal termination. The System.out of the program is stored in the ByteArrayOutputStream that is passsed in.


inputStream2String

public static java.lang.String inputStream2String(java.io.InputStream is)
Converts the InputStream to the returned String.


inputStream2ByteArray

public static java.io.ByteArrayOutputStream inputStream2ByteArray(java.io.InputStream is)
Reads the InputStream into the ByteArrayOutputStream. Wrapper that has a more descriptive name than readInputStream.


readInputStream

public static void readInputStream(java.io.InputStream is,
                                   java.io.ByteArrayOutputStream baos)
Reads the InputStream into the ByteArrayOutputStream.