SPT Core API

com.sptci.io
Class FileUtilities

java.lang.Object
  extended by com.sptci.io.FileUtilities

public final class FileUtilities
extends Object

A utility class that provides static methods for common IO operations.

© Copyright 2005, Sans Pareil Technologies, Inc.

Version:
$Id: FileUtilities.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2005-09-17

Field Summary
static String END_OF_LINE
          The end of line character used by the host operating system.
static String FILE_SEPARATOR
          The path separator character for the host system.
 
Method Summary
static File copy(File source, File destination)
          Copy the contents of the specified source file to the destination file.
static void copy(String source, String destination)
          Copy the contents of the specified source file to the destination file.
static int delete(File directory, boolean deleteDirectory)
          Delete all the files and directories under the specified directory.
static int delete(String directory, boolean deleteDirectory)
          Delete all the files and directories under the specified directory.
static File fromUrl(String url, String... parameters)
          Read the contents of the specified HTTP URL and save to a local File.
static void mkdirs(File file)
          Create the parent directory tree for the specified file if required.
static void setAuthenticator(String userName, String password)
          Create a new instance of Authenticator using the specified credentials.
static void writeToFiles(InputStream inputStream, File[] files)
          Read all the data from the specified InputStream and write them to the specified files.
static void writeToFiles(InputStream inputStream, String[] names)
          Read all the data from the specified InputStream and write them to the specified files.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_SEPARATOR

public static String FILE_SEPARATOR
The path separator character for the host system.


END_OF_LINE

public static String END_OF_LINE
The end of line character used by the host operating system.

Method Detail

copy

public static void copy(String source,
                        String destination)
                 throws IOException
Copy the contents of the specified source file to the destination file.

Parameters:
source - The fully qualified name of the source file that is to be copied.
destination - The fully qualified name of the destination file that is to be created/modified. If the name specified is a directory that exists, then the new file will be created under the directory with the identical name as that of source.
Throws:
IOException - If errors are encountered while copying the file

copy

public static File copy(File source,
                        File destination)
                 throws IOException
Copy the contents of the specified source file to the destination file.

Parameters:
source - The source file that is to be copied.
destination - The destination file that is to be created/modified.
Returns:
The newly copied file for convenience.
Throws:
IOException - If errors are encountered while copying the file
IllegalArgumentException - If the source file does not exist or is a directory.

delete

public static int delete(String directory,
                         boolean deleteDirectory)
                  throws SecurityException
Delete all the files and directories under the specified directory. Invokes delete( File, boolean ) with a new File representing the specified directory.

Parameters:
directory - The directory whose children are to be deleted.
deleteDirectory - Specify true if you wish to delete the specified directory as well. Specify false if you wish to preserve the specified directory.
Returns:
int - Returns the total number of files and directories that were deleted.
Throws:
SecurityException - If the SecurityManager prevented the directory or its children from being deleted.

delete

public static int delete(File directory,
                         boolean deleteDirectory)
                  throws SecurityException
Delete all the files and directories under the specified directory.

Parameters:
directory - The directory whose children are to be deleted.
deleteDirectory - Specify true if you wish to delete the specified directory as well. Specify false if you wish to preserve the specified directory.
Returns:
int - Returns the total number of files and directories that were deleted.
Throws:
SecurityException - If the SecurityManager prevented the directory or its children from being deleted.
See Also:
delete( File )

mkdirs

public static void mkdirs(File file)
Create the parent directory tree for the specified file if required. Check the File.getParentFile() for existence and create if required. This method differs from File.mkdirs() in that it treats the file as a file and not as a directory.

Parameters:
file - The file whose parent directories are to be created.
Throws:
SecurityException - If errors are encountered while creating the directory tree.

fromUrl

public static File fromUrl(String url,
                           String... parameters)
                    throws MalformedURLException,
                           IOException
Read the contents of the specified HTTP URL and save to a local File. The file will be named identical to the name of the file specified in the URL and stored under the current working directory with the same path as on the url resource.

The following code shows sample usage of this method.

 import java.io.File;
 import com.sptci.io.FileUtilities;

 public class test
 {
   public static void main( String[] args )
   {
     try
     {
       final String url = "http://www.sptci.com/index.jsp";
       final String path = "/tmp/web";
       final File file = FileUtilities.fromUrl( url, directory );
       System.out.format( "Downloaded file: %s of size: %d%n",
           file.getName(), file.length() );

       ...
       final String purl = "http://www.sptci.com/private/manual.pdf";
       final String userName = getUserName();
       final String password = getPassword();
       final File another = FileUtilities.fromUrl( purl, null, null, userName, password );
     }
     catch ( Throwable t )
     {
       // Error handling
     }
   }
 }
 

Parameters:
url - The url from which the data is to be fetched.
parameters - Optional parameters used to control the location and name of the downloaded file. Also used to specify the credentials to be used to fetch the file. The parameters should be specified in the following order:
  1. path - The base directory under which the downloaded file is to be saved. Defaults to the path indicated by the url.
  2. name The file name to assign to the downloaded file. Defaults to the file name indicated by the url.
  3. userName The user name part of the credentials to use to authenticate with the server.
  4. password The password to use to authenticate with the server.
Additionally, the userName and password values must always be specified in tandem.
Returns:
The file that was created out of the contents of the url.
Throws:
MalformedURLException - If the specified url is improperly formatted.
IOException - If errors are encountered while reading or writing the data.
Since:
2007-10-28
See Also:
StringUtilities.fromUrl( String ), fromUrl( URL, String, String )

writeToFiles

public static void writeToFiles(InputStream inputStream,
                                String[] names)
                         throws FileNotFoundException,
                                IOException
Read all the data from the specified InputStream and write them to the specified files.

The following code shows sample usage of this method.

 import com.sptci.io.FileUtilities;
 import java.io.BufferedInputStream;
 import java.io.FileInputStream;

 public class test
 {
   public static void main( String[] args )
   {
     try
     {
       String[] names = {"/tmp/one.m4a", "/tmp/two.m4a", "/tmp/three.m4a"};
       BufferedInputStream inputStream = new BufferedInputStream(
           new FileInputStream(
             "/Users/rakesh/Desktop/AaraadayaManu.m4a" ) );
       FileUtilities.writeToFiles( inputStream, names );
     }
     catch ( Throwable t )
     {
       t.printStackTrace();
     }
   }
 }
 

Parameters:
inputStream - The stream from which the data is to be read.
names - The fully qualified names of the files to which the data read is to be written.
Throws:
FileNotFoundException - If the file(s) specified could not be created.
IOException - If errors are encountered while reading the data from the inputStream or while writing to the file(s).
See Also:
TeeOutputStream.TeeOutputStream( String[] ), writeToFiles( InputStream, TeeOutputStream )

writeToFiles

public static void writeToFiles(InputStream inputStream,
                                File[] files)
                         throws FileNotFoundException,
                                IOException
Read all the data from the specified InputStream and write them to the specified files.

Parameters:
inputStream - The stream from which the data is to be read.
files - The files to which the data read is to be written.
Throws:
FileNotFoundException - If the file(s) specified could not be accessed.
IOException - If errors are encountered while reading the data from the inputStream or while writing to the file(s).
See Also:
TeeOutputStream.TeeOutputStream( File[] ), writeToFiles( InputStream, TeeOutputStream )

setAuthenticator

public static void setAuthenticator(String userName,
                                    String password)
Create a new instance of Authenticator using the specified credentials. Create an anonymous sub-class of Authenticator that implements the getPasswordAuthentication method by returning a new instance of PasswordAuthentication using the userName and password specified.

Parameters:
userName - The user name to use for authentication.
password - The password to use for authentication.
Since:
2007-10-28

SPT Core API