com.sibvisions.util.type
Class FileUtil

java.lang.Object
  extended by com.sibvisions.util.type.FileUtil

public final class FileUtil
extends Object

The FileUtil contains file and filename dependent utility methods.


Nested Class Summary
static class FileUtil.CopyMode
          File or directory copy mode.
 
Method Summary
static void copy(File pSource, File pTarget, FileUtil.CopyMode pMode, String... pPatterns)
          Copies one file into another file.
static void copy(File pSource, File pTarget, String... pPatterns)
          Copy a file or directory to another file or directory.
static long copy(InputStream pIn, boolean pCloseIn, OutputStream pOut, boolean pCloseOut)
          Copies the content of an InputStream to the desired OutputStream.
static long copy(InputStream pIn, File pTarget)
          Copies the content from a stream into a file.
static long copy(InputStream pIn, OutputStream pOut)
          Copies the content of an InputStream to the desired OutputStream.
static boolean delete(File pSource, String... pPattern)
          Delete a file or directory.
static boolean deleteEmpty(File pDirectory, String... pPattern)
          Delete empty directories, recursive.
static boolean deleteIfEmpty(File... pDirectory)
          Deletes directories if they are empty.
static boolean deleteSub(File pSource, String... pPattern)
          Delete all files and directories in the given directory.
static String formatAsDirectory(String pPath)
          Formats the given path as directory.
static byte[] getContent(File pFile)
          Gets the content from a file.
static byte[] getContent(InputStream pStream)
          Reads the content of an InputStream into a byte array and closes the stream after reading.
static byte[] getContent(InputStream pStream, boolean pAutoClose)
          Reads the content of an InputStream into a byte array.
static byte[] getContent(InputStreamReader pReader)
          Reads the content of an InputStreamReader into a byte array and closes the stream after reading.
static byte[] getContent(InputStreamReader pReader, boolean pAutoClose)
          Reads the content of an InputStreamReader into a byte array.
static byte[] getContent(String pFileName)
          Gets the content from a file.
static String getDirectory(String pAbsolutePath)
          Gets the directory for a path.
static String getExtension(String pPath)
          Gets the extension of an absolute file path.
static String getName(String pAbsolutePath)
          Gets the name of a file from an absolute path.
static File getNotExistingFile(File pFile)
          Gets a file that does not exist.
static File getNotExistingFile(File pFile, String pOpenBracket, String pCloseBracket)
          Gets a file that does not exist.
static boolean like(String pSource, String pSearch)
          Fast like search in path with wildcard(**, * and ?)
static List<String> listZipEntries(InputStream pArchive)
          Gets a list of all entries from a zip archive.
static void move(File pSource, File pTarget)
          Moves the given source file to another location.
static void move(File pSource, File pTarget, FileUtil.CopyMode pMode)
          Moves the given source file to another location.
static String removeExtension(String pPath)
          Removes the file extension from a file name.
static void replace(File pInput, File pOutput, Hashtable<String,String> pMapping, String pEncoding)
          Replaces key/value mappings read from the given input file and writes the result to the given output file.
static void replace(File pInput, File pOutput, Hashtable<String,String> pMapping, String pInEncoding, String pOutEncoding)
          Replaces key/value mappings read from the given input file and writes the result to the given output file.
static void replace(InputStream pInput, OutputStream pOutput, Hashtable<String,String> pMapping, String pInEncoding, String pOutEncoding)
          Replaces key/value mappings read from the given input stream and writes the result to the given output stream.
static void save(File pFile, byte[] byContent)
          Saves the given content into a file.
static void save(File pFile, InputStream pInput)
          Saves the given stream into a file.
static void unzip(File pFile, String pTarget)
          Unzips the given file.
static void unzip(InputStream pArchive, File pTarget)
          Extracts the content of a zip archive.
static void unzip(InputStream pArchive, String pTarget)
          Extracts the content of a zip archive.
static void unzip(String pFile, String pTarget)
          Unzips the given file.
static void zip(OutputStream pOut, int pMode, File... pFiles)
          Creates a zip package for given files.
static void zip(OutputStream pOut, int pMode, String[] pSearchPath, String... pExtension)
          Creates a zip package for given directories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copy

public static long copy(InputStream pIn,
                        File pTarget)
                 throws IOException
Copies the content from a stream into a file.

Parameters:
pIn - the input stream
pTarget - the output file
Returns:
the number of written bytes
Throws:
IOException - if it's not possible to read from the InputStream or write to the File

copy

public static long copy(InputStream pIn,
                        OutputStream pOut)
                 throws IOException
Copies the content of an InputStream to the desired OutputStream.

Parameters:
pIn - input stream with content
pOut - output stream
Returns:
the length of the InputStream.
Throws:
IOException - if it's not possible to read from the InputStream or write to the OutputStream

copy

public static long copy(InputStream pIn,
                        boolean pCloseIn,
                        OutputStream pOut,
                        boolean pCloseOut)
                 throws IOException
Copies the content of an InputStream to the desired OutputStream.

Parameters:
pIn - input stream with content
pCloseIn - true, if the input stream
pOut - output stream
pCloseOut - true, if the output stream
Returns:
the length of the InputStream.
Throws:
IOException - if it's not possible to read from the InputStream or write to the OutputStream

copy

public static void copy(File pSource,
                        File pTarget,
                        String... pPatterns)
                 throws IOException
Copy a file or directory to another file or directory.

Parameters:
pSource - the source file
pTarget - the target file
pPatterns - the include/exclude patterns. This parameter is only used for directory copy.
Throws:
IOException - if the copy process failed

copy

public static void copy(File pSource,
                        File pTarget,
                        FileUtil.CopyMode pMode,
                        String... pPatterns)
                 throws IOException
Copies one file into another file. If the source file is a directory, then all files in the directory will be copied to the target.

Parameters:
pSource - source file
pTarget - target file
pMode - the copy mode
pPatterns - include/exclude file pattern(s)
Throws:
IOException - if it's not possible to copy the file or directory

move

public static void move(File pSource,
                        File pTarget)
                 throws IOException
Moves the given source file to another location. If the target file exists, it will be overwritten.

Parameters:
pSource - the source file
pTarget - the target location
Throws:
IOException - if an error occurs during moving

move

public static void move(File pSource,
                        File pTarget,
                        FileUtil.CopyMode pMode)
                 throws IOException
Moves the given source file to another location. The given copy mode defines the operation if the target file already exists.

Parameters:
pSource - the source file
pTarget - the target location
pMode - the copy mode
Throws:
IOException - if an error occurs during moving

getContent

public static byte[] getContent(InputStream pStream)
Reads the content of an InputStream into a byte array and closes the stream after reading.

Parameters:
pStream - the input stream
Returns:
the content of the stream or null if an error occurs

getContent

public static byte[] getContent(InputStream pStream,
                                boolean pAutoClose)
Reads the content of an InputStream into a byte array.

Parameters:
pStream - the input stream
pAutoClose - whether the input stream should be closed after reading
Returns:
the content of the stream or null if an error occurs

getContent

public static byte[] getContent(File pFile)
                         throws IOException
Gets the content from a file.

Parameters:
pFile - the file
Returns:
the content of the file or null if an error occurs
Throws:
IOException - if the file does not exist

getContent

public static byte[] getContent(String pFileName)
                         throws IOException
Gets the content from a file.

Parameters:
pFileName - the path to the file
Returns:
the content of the file or null if an error occurs
Throws:
IOException - if the file does not exist

getContent

public static byte[] getContent(InputStreamReader pReader)
Reads the content of an InputStreamReader into a byte array and closes the stream after reading.

Parameters:
pReader - the input stream reader
Returns:
the content of the reader or null if an error occurs

getContent

public static byte[] getContent(InputStreamReader pReader,
                                boolean pAutoClose)
Reads the content of an InputStreamReader into a byte array.

Parameters:
pReader - the input stream reader
pAutoClose - whether the input stream should be closed after reading
Returns:
the content of the reader or null if an error occurs

removeExtension

public static String removeExtension(String pPath)
Removes the file extension from a file name.

Parameters:
pPath - the filename
Returns:
the filename without extension

getExtension

public static String getExtension(String pPath)
Gets the extension of an absolute file path.

Parameters:
pPath - the absolute path of a file
Returns:
the extension (text behind the last '.') or null if the path is null or has no extension

getNotExistingFile

public static File getNotExistingFile(File pFile)
Gets a file that does not exist. If the given file already exists, the file name built in the way "nameWithoutExtension[number].extension".

Parameters:
pFile - the desired file name
Returns:
a file that does not exist.

getNotExistingFile

public static File getNotExistingFile(File pFile,
                                      String pOpenBracket,
                                      String pCloseBracket)
Gets a file that does not exist. If the given file already exists, the file name built in the way "nameWithoutExtension<bracketstart>number<bracketend>.extension".

Parameters:
pFile - the desired file name
pOpenBracket - the start bracket
pCloseBracket - the close bracket
Returns:
a file that does not exist.

getName

public static String getName(String pAbsolutePath)
Gets the name of a file from an absolute path.

Parameters:
pAbsolutePath - the absolute path for a file
Returns:
the name of the file without path

getDirectory

public static String getDirectory(String pAbsolutePath)
Gets the directory for a path. It searchs the last path separator and cuts off all characters behind, e.g. /home/a/b/c returns /home/a/b

Parameters:
pAbsolutePath - a path
Returns:
the parent directory path or null if no path separator was found

formatAsDirectory

public static String formatAsDirectory(String pPath)
Formats the given path as directory. That means a trailing slash will be added if the path is not already a directory.

Parameters:
pPath - the path
Returns:
the directory path

deleteSub

public static boolean deleteSub(File pSource,
                                String... pPattern)
Delete all files and directories in the given directory.

Parameters:
pSource - the file or directory
pPattern - the patterns
Returns:
true if the directory was deleted, false otherwise

delete

public static boolean delete(File pSource,
                             String... pPattern)
Delete a file or directory.

Parameters:
pSource - the file or directory
pPattern - the patterns
Returns:
true if the directory was deleted, false otherwise

deleteEmpty

public static boolean deleteEmpty(File pDirectory,
                                  String... pPattern)
Delete empty directories, recursive.

Parameters:
pDirectory - the start directories
pPattern - the include/exclude pattern
Returns:
true if all empty directories were deleted, false otherwise

deleteIfEmpty

public static boolean deleteIfEmpty(File... pDirectory)
Deletes directories if they are empty.

Parameters:
pDirectory - a list of directories
Returns:
true if all empty directories were deleted, false if an empty directory can not be deleted

zip

public static void zip(OutputStream pOut,
                       int pMode,
                       String[] pSearchPath,
                       String... pExtension)
                throws IOException
Creates a zip package for given directories.

Parameters:
pOut - the output stream
pMode - ZipOutputStream.STORED or ZipOutputStream.DEFLATED
pSearchPath - all search path
pExtension - all allowed extensions
Throws:
IOException - if a problem occurs during zip creation

zip

public static void zip(OutputStream pOut,
                       int pMode,
                       File... pFiles)
                throws IOException
Creates a zip package for given files.

Parameters:
pOut - the output stream
pMode - ZipOutputStream.STORED or ZipOutputStream.DEFLATED
pFiles - all search path
Throws:
IOException - if a problem occurs during zip creation

unzip

public static void unzip(String pFile,
                         String pTarget)
                  throws IOException
Unzips the given file.

Parameters:
pFile - the path to the zip archive
pTarget - the target directory
Throws:
IOException - if unzipping fails

unzip

public static void unzip(File pFile,
                         String pTarget)
                  throws IOException
Unzips the given file.

Parameters:
pFile - the zip archive
pTarget - the target directory
Throws:
IOException - if unzipping fails

unzip

public static void unzip(InputStream pArchive,
                         String pTarget)
                  throws IOException
Extracts the content of a zip archive.

Parameters:
pArchive - the zip archive
pTarget - where the content should go
Throws:
IOException - if an error occurs during extraction
See Also:
unzip(InputStream, File)

unzip

public static void unzip(InputStream pArchive,
                         File pTarget)
                  throws IOException
Extracts the content of a zip archive.

Parameters:
pArchive - the zip archive
pTarget - where the content should go
Throws:
IOException - if an error occurs during extraction

listZipEntries

public static List<String> listZipEntries(InputStream pArchive)
                                   throws IOException
Gets a list of all entries from a zip archive.

Parameters:
pArchive - the stream for the zip archive
Returns:
the list of entries
Throws:
IOException - if the archive does not contain zip content or a read error occurs

save

public static void save(File pFile,
                        byte[] byContent)
                 throws IOException
Saves the given content into a file.

Parameters:
pFile - the output file
byContent - the data
Throws:
IOException - if an error occurs during output

save

public static void save(File pFile,
                        InputStream pInput)
                 throws IOException
Saves the given stream into a file.

Parameters:
pFile - the output file
pInput - the input stream
Throws:
IOException - if an error occurs during output

replace

public static void replace(File pInput,
                           File pOutput,
                           Hashtable<String,String> pMapping,
                           String pEncoding)
                    throws IOException
Replaces key/value mappings read from the given input file and writes the result to the given output file. This method requires ASCII files. If the given input and output file are the same, then this methods caches the data in a temporary file or in memory before writing the result to the output file. An input file with a filesize smaller than 1M is cached in memory.

Parameters:
pInput - the input file
pOutput - the output file
pMapping - the key/value mapping e.g. ${PARAM1} = JVx
pEncoding - the encoding for the input/output files
Throws:
IOException - if a read or write error occurs

replace

public static void replace(File pInput,
                           File pOutput,
                           Hashtable<String,String> pMapping,
                           String pInEncoding,
                           String pOutEncoding)
                    throws IOException
Replaces key/value mappings read from the given input file and writes the result to the given output file. This method requires ASCII files. If the given input and output file are the same, then this methods caches the data in a temporary file or in memory before writing the result to the output file. An input file with a filesize smaller than 1M is cached in memory.

Parameters:
pInput - the input file
pOutput - the output file
pMapping - the key/value mapping e.g. ${PARAM1} = JVx
pInEncoding - the encoding of the input file
pOutEncoding - the encoding for the output file
Throws:
IOException - if a read or write error occurs

replace

public static void replace(InputStream pInput,
                           OutputStream pOutput,
                           Hashtable<String,String> pMapping,
                           String pInEncoding,
                           String pOutEncoding)
                    throws IOException
Replaces key/value mappings read from the given input stream and writes the result to the given output stream. This method requires ASCII streams.

Parameters:
pInput - the input stream
pOutput - the output stream
pMapping - the key/value mapping e.g. ${PARAM1} = JVx
pInEncoding - the encoding of the input stream
pOutEncoding - the encoding for the output stream
Throws:
IOException - if a read or write error occurs

like

public static boolean like(String pSource,
                           String pSearch)
Fast like search in path with wildcard(**, * and ?) support.

Parameters:
pSource - any string
pSearch - search pattern with or without wildcards.
Returns:
true if, and only if, the string matches the pattern


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.