org.varienaja.util
Class FileOperations

java.lang.Object
  extended by org.varienaja.util.FileOperations

public class FileOperations
extends java.lang.Object

Some much-used operations on files. TODO Add a logger, don't print stacktraces.

Author:
Varienaja

Constructor Summary
FileOperations()
           
 
Method Summary
static java.lang.String copyFile(java.lang.String src, java.lang.String dstdir, java.lang.String dst)
          Copies a file.
static void copyStream(java.io.InputStream in, java.io.OutputStream out)
           
static java.lang.String createUniqueFilename(java.lang.String dstdir, java.lang.String dst)
          Finds a filename that is not already used.
static boolean deleteDir(java.io.File dir)
          Deletes a directory including all contents.
static boolean deleteFile(java.lang.String filename)
          Deletes a file from the filesystem.
static int extractToDirectory(TolerantZipInputStream zis, java.lang.String directory)
          Takes a zip-inputstream and extracts the contents to the specified directory.
static java.lang.String moveFile(java.lang.String src, java.lang.String dstdir, java.lang.String dst)
          Moves a file.
static java.lang.String streamToFile(java.io.InputStream in, java.io.File file)
          Saves an InputStream to a file.
static java.lang.String streamToFile(java.io.InputStream in, java.lang.String filename)
          Saves an InputStream to a file.
static java.lang.String tmpFile(java.io.InputStream in, java.lang.String tmpname)
          Creates a file in the temp-directory and returns the filename
static java.lang.String translateIllegalDirectoryChars(java.lang.String from)
          Returns the "from" string with all : * ? " < > | characters replaced with "_".
static java.lang.String translateIllegalFileChars(java.lang.String from)
          Returns the "from" string with all / \ : * ? " < > | characters replaced with "_".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileOperations

public FileOperations()
Method Detail

createUniqueFilename

public static java.lang.String createUniqueFilename(java.lang.String dstdir,
                                                    java.lang.String dst)
Finds a filename that is not already used. This method does not create the file for you, so it is theoretically possible that the returned available filename is already taken.

Parameters:
dstdir - The directory where the file should be created.
dst - The desired filename
Returns:
The desider filename, if still available. Or an alternative where needed.

copyFile

public static java.lang.String copyFile(java.lang.String src,
                                        java.lang.String dstdir,
                                        java.lang.String dst)
                                 throws java.io.IOException
Copies a file. If the destination-directory contains illegal characters, they will be replaced by underscores. If the filename contains illegal characters, they will be replaced by underscores. If the target file already exists, a new filename will be generated.

Parameters:
src - The location of the sourcefile.
dstdir - The target directory.
dst - The requested filename (without directory-part).
Returns:
The filename, that the file was finally copied to.
Throws:
java.io.IOException

moveFile

public static java.lang.String moveFile(java.lang.String src,
                                        java.lang.String dstdir,
                                        java.lang.String dst)
                                 throws java.io.IOException
Moves a file. Behaves just like copyFile ( @see copyFile ).

Parameters:
src -
dstdir -
dst -
Returns:
Throws:
java.io.IOException

copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out)

tmpFile

public static java.lang.String tmpFile(java.io.InputStream in,
                                       java.lang.String tmpname)
                                throws java.io.IOException
Creates a file in the temp-directory and returns the filename

Parameters:
in - The InputStream
tmpname - The filename to use (without path)
Returns:
The fully qualified filename
Throws:
java.io.IOException

streamToFile

public static java.lang.String streamToFile(java.io.InputStream in,
                                            java.lang.String filename)
                                     throws java.io.IOException
Saves an InputStream to a file. If the file does not exist, it is created. If the file already exists, it is overwritten!

Parameters:
in - The InputStream
filename - The file to save the Stream into.
Returns:
The filename
Throws:
java.io.IOException

streamToFile

public static java.lang.String streamToFile(java.io.InputStream in,
                                            java.io.File file)
                                     throws java.io.IOException
Saves an InputStream to a file. If the file does not exist, it is created. If the file already exists, it is overwritten!

Parameters:
in - The InputStream
filename - The file to save the Stream into.
Returns:
The file
Throws:
java.io.IOException

deleteFile

public static boolean deleteFile(java.lang.String filename)
Deletes a file from the filesystem.

Parameters:
filename - The file to delete
Returns:
True, if succesfull; False otherwise.

deleteDir

public static boolean deleteDir(java.io.File dir)
Deletes a directory including all contents.

WARNING: This method will happily delete the contents of your entiry hard drive. Use with extreme care.

Parameters:
dir - The directory to delete.
Returns:
True if the directory was deleted succesfully, false otherwise.

translateIllegalFileChars

public static java.lang.String translateIllegalFileChars(java.lang.String from)
Returns the "from" string with all / \ : * ? " < > | characters replaced with "_". In addition, points (.) and spaces are not allowed as first or last character of the filename.

Parameters:
from - The source string
Returns:
The source string with every occurence of / \ : * ? " < > | replaced with "_".

translateIllegalDirectoryChars

public static java.lang.String translateIllegalDirectoryChars(java.lang.String from)
Returns the "from" string with all : * ? " < > | characters replaced with "_". In addition, points (.) and spaces are not allowed as first or last character of the directory name.

Parameters:
from - The source string
Returns:
The source string with every occurence of : * ? " < > | replaced with "_".

extractToDirectory

public static int extractToDirectory(TolerantZipInputStream zis,
                                     java.lang.String directory)
Takes a zip-inputstream and extracts the contents to the specified directory. If files in the zipstream are already present at the specifiek location, a new name for the file will be created using the createUniqueFilename-method.

Parameters:
zis - The Zip-inputstream
directory - The location to unzip to
Returns:
The number of files (excluding directories) exracted
See Also:
createUniqueFilename


Copyright © 2010 A.J.V.. All Rights Reserved.