|
SPT Core API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sptci.io.FileUtilities
public final class FileUtilities
A utility class that provides static methods for
common IO operations.
© Copyright 2005, Sans Pareil Technologies, Inc.
| 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 |
|---|
public static String FILE_SEPARATOR
public static String END_OF_LINE
| Method Detail |
|---|
public static void copy(String source,
String destination)
throws IOException
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.
IOException - If errors are encountered while copying the file
public static File copy(File source,
File destination)
throws IOException
source - The source file that is to be copied.destination - The destination file that is to be created/modified.
IOException - If errors are encountered while copying the file
IllegalArgumentException - If the source file does not exist
or is a directory.
public static int delete(String directory,
boolean deleteDirectory)
throws SecurityException
delete( File, boolean ) with a new File representing the specified directory.
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.
SecurityException - If the SecurityManager
prevented the directory or its children from being deleted.
public static int delete(File directory,
boolean deleteDirectory)
throws SecurityException
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.
SecurityException - If the SecurityManager
prevented the directory or its children from being deleted.delete( File )public static void mkdirs(File file)
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.
file - The file whose parent directories are to be created.
SecurityException - If errors are encountered while creating
the directory tree.
public static File fromUrl(String url,
String... parameters)
throws MalformedURLException,
IOException
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
}
}
}
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:
path - The base directory under which the downloaded
file is to be saved. Defaults to the path indicated by the url.name The file name to assign to the downloaded file.
Defaults to the file name indicated by the url.userName The user name part of the credentials to
use to authenticate with the server.password The password to use to authenticate with
the server.userName and password
values must always be specified in tandem.
url.
MalformedURLException - If the specified url
is improperly formatted.
IOException - If errors are encountered while reading or writing
the data.StringUtilities.fromUrl( String ),
fromUrl( URL, String, String )
public static void writeToFiles(InputStream inputStream,
String[] names)
throws FileNotFoundException,
IOException
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();
}
}
}
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.
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).TeeOutputStream.TeeOutputStream( String[] ),
writeToFiles( InputStream, TeeOutputStream )
public static void writeToFiles(InputStream inputStream,
File[] files)
throws FileNotFoundException,
IOException
InputStream and
write them to the specified files.
inputStream - The stream from which the data is to be read.files - The files to which the data read is to be written.
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).TeeOutputStream.TeeOutputStream( File[] ),
writeToFiles( InputStream, TeeOutputStream )
public static void setAuthenticator(String userName,
String password)
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.
userName - The user name to use for authentication.password - The password to use for authentication.
|
SPT Core API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||