com.resilientedge.util.io
Class FileHelper

java.lang.Object
  |
  +--com.resilientedge.util.io.FileHelper

public final class FileHelper
extends java.lang.Object

Some file and resource utilities like reading the entire file into a string, reading the file from anywhere in the classpath, etc.

Author:
jwh

Constructor Summary
FileHelper()
           
 
Method Summary
static java.io.File createFile(java.lang.String filePath, boolean overwrite)
          Creates a new file given the string filename...
static void delete(java.lang.String fileName)
          Delete the specified file
static boolean exists(java.lang.String fileName)
          Does the specified file exist?
static java.io.InputStream getResource(java.lang.String fileName)
          Get the input stream of the file whose name is fileName.
static java.lang.String read(java.io.File file)
          Read the specified file into a string
static java.lang.String read(java.io.Reader reader)
          Read from the specified reader into a string
static java.lang.String read(java.lang.String fileName)
          Read the specified file into a string
static byte[] readBytes(java.io.File file)
          Read the specified file into a string
static java.lang.String readResource(java.lang.String fileName)
          Get the contents of the file whose name is fileName.
static java.lang.String readUrl(java.lang.String url)
          Read the specified url into a string
static java.lang.String readUrl(java.net.URL url)
          Read the specified url into a string
static byte[] streamToByteArray(java.io.InputStream is)
          Read a stream into a byte array
static void write(java.io.File file, byte[] data)
          Write the string to the specified file
static void write(java.io.File file, java.lang.String data)
          Write the string to the specified file
static void write(java.lang.String file, java.lang.String data)
          Write the string to the specified file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileHelper

public FileHelper()
Method Detail

getResource

public static java.io.InputStream getResource(java.lang.String fileName)
                                       throws java.io.IOException
Get the input stream of the file whose name is fileName. The file must be in the classpath (this allows the use of relative file names).
Parameters:
fileName - Parameter Description
Returns:
The Resource value
Throws:
java.io.IOException - Exception Description

exists

public static boolean exists(java.lang.String fileName)
Does the specified file exist?
Parameters:
fileName - Parameter Description
Returns:
Returned Value Description

read

public static java.lang.String read(java.lang.String fileName)
                             throws java.io.IOException
Read the specified file into a string
Parameters:
fileName - Parameter Description
Returns:
Returned Value Description
Throws:
java.io.IOException - Exception Description

readResource

public static java.lang.String readResource(java.lang.String fileName)
                                     throws java.io.IOException
Get the contents of the file whose name is fileName. The file must be in the classpath (this allows the use of relative file names).
Parameters:
fileName - Parameter Description
Returns:
Returned Value Description
Throws:
java.io.IOException - Exception Description

read

public static java.lang.String read(java.io.File file)
                             throws java.io.IOException
Read the specified file into a string
Parameters:
file - Parameter Description
Returns:
Returned Value Description
Throws:
java.io.IOException - Exception Description

readUrl

public static java.lang.String readUrl(java.lang.String url)
                                throws java.io.IOException
Read the specified url into a string
Parameters:
url - the url to read
Returns:
the contents of the url in a string
Throws:
java.io.IOException - Description of Exception

readUrl

public static java.lang.String readUrl(java.net.URL url)
                                throws java.io.IOException
Read the specified url into a string
Parameters:
url - the url to read
Returns:
the contents of the url in a string
Throws:
java.io.IOException - Description of Exception

read

public static java.lang.String read(java.io.Reader reader)
                             throws java.io.IOException
Read from the specified reader into a string
Parameters:
reader - Parameter Description
Returns:
Returned Value Description
Throws:
java.io.IOException - Exception Description

write

public static void write(java.lang.String file,
                         java.lang.String data)
                  throws java.io.IOException
Write the string to the specified file
Parameters:
file - Parameter Description
data - Parameter Description
Throws:
java.io.IOException - Exception Description

write

public static void write(java.io.File file,
                         java.lang.String data)
                  throws java.io.IOException
Write the string to the specified file
Parameters:
file - Parameter Description
data - Parameter Description
Throws:
java.io.IOException - Exception Description

write

public static void write(java.io.File file,
                         byte[] data)
                  throws java.io.IOException
Write the string to the specified file
Parameters:
file - Parameter Description
data - Parameter Description
Throws:
java.io.IOException - Exception Description

delete

public static void delete(java.lang.String fileName)
                   throws java.io.IOException
Delete the specified file
Parameters:
fileName - Parameter Description
Throws:
java.io.IOException - Exception Description

readBytes

public static byte[] readBytes(java.io.File file)
                        throws java.io.IOException
Read the specified file into a string
Parameters:
file - Parameter Description
Returns:
Returned Value Description
Throws:
java.io.IOException - Exception Description

streamToByteArray

public static byte[] streamToByteArray(java.io.InputStream is)
Read a stream into a byte array
Parameters:
is - Parameter Description
Returns:
an array of the contents found in the stream

createFile

public static java.io.File createFile(java.lang.String filePath,
                                      boolean overwrite)
Creates a new file given the string filename... will create any directories in the path of the fileName Example filePath = "dir1/dir2/dir3/dir4/filename" will create all of the directories that do not exist and then create the file "filename" and return it.
Parameters:
filePath - the path of the file to create, may include directories
overwrite - whether or not to overwite the file if it already exists
Returns:
The newly created file


Copyright © 2001 Resilient Edge, Inc. All Rights Reserved.