com.funambol.syncclient.common
Class FileSystemTools

java.lang.Object
  extended by com.funambol.syncclient.common.FileSystemTools

public class FileSystemTools
extends java.lang.Object

This class supplies some methods of usefullness for the management of files and directories.

In particular it supplies methods for the creation and the reading of file and for the removal of a directory and of its everything content

Version:
$Id: FileSystemTools.java,v 1.3 2007-12-22 18:09:17 nichele Exp $

Constructor Summary
FileSystemTools()
           
 
Method Summary
static void createFile(java.lang.String directoryName, java.lang.String fileName, byte[] content)
          Create a new file in the specified directory using the given file name and content.
static void createFile(java.lang.String directoryName, java.lang.String fileName, java.io.ByteArrayOutputStream byteStream)
          Create a new file in the specified directory using the given file name and byteStream.
static java.lang.String[] getAllFiles(java.lang.String directory, java.lang.String extension)
          Returns all files in the given directory filtering on the given extension.
static byte[] getFile(java.lang.String fileName)
          Returns the content of the file specified.
static byte[] readFileBytes(java.io.File file)
          Reads a file into a byte array given its filename
static byte[] readFileBytes(java.lang.String filename)
          Reads a file into a byte array given its filename
static java.lang.String readFileString(java.io.File file)
          Reads a file into a String given its filename
static java.lang.String readFileString(java.io.InputStream is)
          Reads a file into a String given an input stream.
static java.lang.String readFileString(java.lang.String filename)
          Reads a file into a String given its filename
static void removeDirectoryTree(java.lang.String directoryName)
          Delete a directory, his subdirectory and all files.
static void writeFile(byte[] buf, java.io.File file)
          Writes the given bytes to the file with the given name
static void writeFile(byte[] buf, java.lang.String filename)
          Writes the given bytes to the file with the given name
static void writeFile(java.lang.String str, java.io.File file)
          Writes the given string to the file with the given name
static void writeFile(java.lang.String str, java.lang.String filename)
          Writes the given string to the file with the given name
static void writeTextFile(java.io.File file, java.lang.String text, boolean append)
          Write the given text in the given file appending to or rewriting it accordingly to append.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileSystemTools

public FileSystemTools()
Method Detail

removeDirectoryTree

public static void removeDirectoryTree(java.lang.String directoryName)
                                throws java.lang.Exception
Delete a directory, his subdirectory and all files.

Parameters:
directoryName - the name of the directory to remove.
Throws:
java.lang.Exception - if an error occurs.

createFile

public static void createFile(java.lang.String directoryName,
                              java.lang.String fileName,
                              byte[] content)
                       throws java.lang.Exception
Create a new file in the specified directory using the given file name and content.

If the directory it does not exist it will be created.

If already exists a file with the same name, this has deleted before create the new file.

Parameters:
directoryName - the directory in which the file must be created.
fileName - the name of the file
content - the content of the file.
Throws:
java.lang.Exception - if an error occurs during creation.

createFile

public static void createFile(java.lang.String directoryName,
                              java.lang.String fileName,
                              java.io.ByteArrayOutputStream byteStream)
                       throws java.lang.Exception
Create a new file in the specified directory using the given file name and byteStream.

If the directory it does not exist it will be created.

If already exists a file with the same name, this has deleted before create the new file.

Parameters:
directoryName - the directory in which the file must be created.
fileName - the name of the file
byteStream - the content of the file.
Throws:
java.lang.Exception - if an error occurs during creation.

writeTextFile

public static void writeTextFile(java.io.File file,
                                 java.lang.String text,
                                 boolean append)
                          throws java.io.IOException
Write the given text in the given file appending to or rewriting it accordingly to append.

Parameters:
file - the file to write to - NOT NULL
text - the text to write - NULL, equivalent to blank
append - if true the text is appended to the file, if false the file is overwritten
Throws:
java.io.IOException - if an error occurs

getFile

public static byte[] getFile(java.lang.String fileName)
                      throws java.io.IOException
Returns the content of the file specified.

Parameters:
fileName - the name of the file.
Returns:
the content of the file.
Throws:
java.io.IOException - if the file not exits or if an I/O error occurs.

getAllFiles

public static java.lang.String[] getAllFiles(java.lang.String directory,
                                             java.lang.String extension)
Returns all files in the given directory filtering on the given extension. Pathnames are returned relative to directory

Parameters:
directory - the directory to list
extension - select only the files with this extension
Returns:
String[] with the selected filenames. An zero-length array if no files are selected

writeFile

public static void writeFile(java.lang.String str,
                             java.io.File file)
                      throws java.io.IOException
Writes the given string to the file with the given name

Parameters:
str - the string to write
file - the file name as a java.io.File
Throws:
java.io.IOException

writeFile

public static void writeFile(java.lang.String str,
                             java.lang.String filename)
                      throws java.io.IOException
Writes the given string to the file with the given name

Parameters:
str - the string to write
filename - the file name as a java.lang.String
Throws:
java.io.IOException

writeFile

public static void writeFile(byte[] buf,
                             java.lang.String filename)
                      throws java.io.IOException
Writes the given bytes to the file with the given name

Parameters:
buf - the bytes to write
filename - the file name as a java.lang.String
Throws:
java.io.IOException

writeFile

public static void writeFile(byte[] buf,
                             java.io.File file)
                      throws java.io.IOException
Writes the given bytes to the file with the given name

Parameters:
buf - the bytes to write
file - the file name as a java.io.File
Throws:
java.io.IOException

readFileBytes

public static byte[] readFileBytes(java.io.File file)
                            throws java.io.IOException
Reads a file into a byte array given its filename

Parameters:
file - the filename (as java.io.File)
Returns:
the content of the file as a byte array
Throws:
java.io.IOException;
java.io.IOException

readFileBytes

public static byte[] readFileBytes(java.lang.String filename)
                            throws java.io.IOException
Reads a file into a byte array given its filename

Parameters:
filename - the filename (as java.lang.String)
Returns:
the content of the file as a byte array
Throws:
java.io.IOException;
java.io.IOException

readFileString

public static java.lang.String readFileString(java.io.File file)
                                       throws java.io.IOException
Reads a file into a String given its filename

Parameters:
file - the filename (as java.io.File)
Returns:
the content of the file as a string
Throws:
java.io.IOException;
java.io.IOException

readFileString

public static java.lang.String readFileString(java.lang.String filename)
                                       throws java.io.IOException
Reads a file into a String given its filename

Parameters:
filename - the filename (as java.lang.String)
Returns:
the content of the file as a string
Throws:
java.io.IOException;
java.io.IOException

readFileString

public static java.lang.String readFileString(java.io.InputStream is)
                                       throws java.io.IOException
Reads a file into a String given an input stream. It is responsibility of the caller to close the stream when not used anymore.

Parameters:
is - the InputStream to read
Returns:
the content of the stream
Throws:
java.io.IOException;
java.io.IOException


Copyright © 2010 Funambol. All Rights Reserved.