Rakesh API

org.rakeshv.io
Class FileUtilities

java.lang.Object
  extended by org.rakeshv.io.FileUtilities

public final class FileUtilities
extends java.lang.Object

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

© Copyright 2005, Rakesh Vidyadharan

Version:
$Id: FileUtilities.java,v 1.3 2005/10/18 01:36:24 rakesh Exp $
Author:
Rakesh Vidyadharan 17th September 2005

Constructor Summary
private FileUtilities()
          Default constructor.
 
Method Summary
private static boolean delete(java.io.File file)
          Delete the specified file.
static int delete(java.io.File directory, boolean deleteDirectory)
          Delete all the files and directories under the specified directory.
static int delete(java.lang.String directory, boolean deleteDirectory)
          Delete all the files and directories under the specified directory.
static void mkdirs(java.io.File file)
          Create the parent directory tree for the specified file if required.
static void writeToFiles(java.io.InputStream inputStream, java.io.File[] files)
          Read all the data from the specified InputStream and write them to the specified files.
static void writeToFiles(java.io.InputStream inputStream, java.lang.String[] names)
          Read all the data from the specified InputStream and write them to the specified files.
private static void writeToFiles(java.io.InputStream inputStream, TeeOutputStream tee)
          Read all the data from the specified InputStream and write them to the specified TeeOutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtilities

private FileUtilities()
Default constructor. Cannot be instantiated.

Method Detail

delete

public static int delete(java.lang.String directory,
                         boolean deleteDirectory)
                  throws java.lang.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:
java.lang.SecurityException - If the SecurityManager prevented the directory or its children from being deleted.

delete

public static int delete(java.io.File directory,
                         boolean deleteDirectory)
                  throws java.lang.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:
java.lang.SecurityException - If the SecurityManager prevented the directory or its children from being deleted.
See Also:
delete( File )

mkdirs

public static final void mkdirs(java.io.File file)
Create the parent directory tree for the specified file if required. Check the parent for existence and create if required.

Throws:
java.lang.SecurityException - If errors are encountered while creating the directory tree.

writeToFiles

public static final void writeToFiles(java.io.InputStream inputStream,
                                      java.lang.String[] names)
                               throws java.io.FileNotFoundException,
                                      java.io.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 org.rakeshv.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:
java.io.FileNotFoundException - If the file(s) specified could not be created.
java.io.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 final void writeToFiles(java.io.InputStream inputStream,
                                      java.io.File[] files)
                               throws java.io.FileNotFoundException,
                                      java.io.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:
java.io.FileNotFoundException - If the file(s) specified could not be accessed.
java.io.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 )

writeToFiles

private static final void writeToFiles(java.io.InputStream inputStream,
                                       TeeOutputStream tee)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Read all the data from the specified InputStream and write them to the specified TeeOutputStream.

Parameters:
inputStream - The stream from which the data is to be read.
tee - The tee to which the data read is to be written.
Throws:
java.io.IOException - If errors are encountered while reading the data from the inputStream or while writing to the file(s).
java.io.FileNotFoundException

delete

private static boolean delete(java.io.File file)
                       throws java.lang.SecurityException
Delete the specified file. Log a message to System.err if errors were encountered while deleting the file.

Parameters:
file - The file which is to be deleted.
Returns:
boolean - Returns false if the file could not be deleted.
Throws:
java.lang.SecurityException - If the SecurityManager prevented the file from being deleted.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan