com.neeve.util
Class UtlFile

java.lang.Object
  extended by com.neeve.util.UtlFile

public final class UtlFile
extends Object

Utility class containing methods to work with files and directories.

This class MAY be used by end users, however it is not supported.


Field Summary
static String FILE_USENATIVE_PROPNAME
           
 
Constructor Summary
UtlFile()
           
 
Method Summary
static void copyDirectory(File source, File dest)
          Recursively copy a directory.
static void copyDirectory(File source, File dest, FilenameFilter filter)
          Recursively copy filtered contents of a directory.
static void copyDirectory(File source, File dest, FilenameFilter filter, boolean recursive)
          Copy a directory.
static void copyFile(File source, File dest)
          Copy a file
static void copyFile(File source, File dest, Properties replacementMap)
           
static void copyFileWithNamespaceToDirectory(File source, File destination, String namespace, String nameWithExtension)
          Copies file to multiple directory, creating it if it doesn't exist.
static void copyToFile(InputStream is, File target)
           
static File copyToTempFile(InputStream is)
           
static void deleteDirectory(File dir)
           
static String expandPath(String path)
          Expands the given String path with any path variables such as user home directory (e.g.
static Collection<File> findFilesByEnding(File dir, String ending)
          Searches a directory recursively to find files that match given ending, ignoring case.
static String getUserHome()
          Returns the current user's home directory.
static File getUserHomeDir()
          Returns the current user's home directory.
static boolean isNativeFileIOEnabled()
           
static void nativeDestroy(long nfile)
          Destroy a native file object
static long nativeWrap(int fd)
          Wrap a native file object around a file descriptor
static long nativeWrite(long nsc, ByteBuffer[] buffers, int count)
          Write a set of byte buffers to a native file
static File newDirWithNamespace(File parent, String namespace)
          Creates File instance of directory with dot-separated namespace appended to the path.
static File newFileWithNamespace(File parent, String namespace, String nameWithExtension)
          Equivalent to calling newFileWithNamespace(File, String, String, String) with extension = null.
static File newFileWithNamespace(File parent, String namespace, String name, String extension)
          Convenience helper for constructing File instances that involve java-style namespaces as their segment.
static String readableFileSize(long size)
           
static String readFile(File file, String charset)
          Reads a file into a String.
static String relativePath(File directory, File file)
          Gets the path to the given file relative to the given directory.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_USENATIVE_PROPNAME

public static final String FILE_USENATIVE_PROPNAME
See Also:
Constant Field Values
Constructor Detail

UtlFile

public UtlFile()
Method Detail

isNativeFileIOEnabled

public static final boolean isNativeFileIOEnabled()
Returns:
true if native file I/O is enabled.

nativeWrap

public static final long nativeWrap(int fd)
                             throws Exception
Wrap a native file object around a file descriptor

Throws:
Exception

nativeWrite

public static final long nativeWrite(long nsc,
                                     ByteBuffer[] buffers,
                                     int count)
                              throws Exception
Write a set of byte buffers to a native file

Throws:
Exception

nativeDestroy

public static final void nativeDestroy(long nfile)
Destroy a native file object


copyFile

public static final void copyFile(File source,
                                  File dest)
                           throws IOException
Copy a file

Throws:
IOException

copyFileWithNamespaceToDirectory

public static final void copyFileWithNamespaceToDirectory(File source,
                                                          File destination,
                                                          String namespace,
                                                          String nameWithExtension)
                                                   throws IOException
Copies file to multiple directory, creating it if it doesn't exist.

Parameters:
source - Source file to copy.
destination - Destination directory.
namespace - dot-separated namespace for destination file. Will be converted to file path segment prepended to file name.
nameWithExtension - File name with extension.
Throws:
IOException - if file read/write operation fails.

copyFile

public static final void copyFile(File source,
                                  File dest,
                                  Properties replacementMap)
                           throws IOException
Throws:
IOException

copyToTempFile

public static final File copyToTempFile(InputStream is)
                                 throws IOException
Throws:
IOException

copyToFile

public static final void copyToFile(InputStream is,
                                    File target)
                             throws IOException
Throws:
IOException

copyDirectory

public static final void copyDirectory(File source,
                                       File dest,
                                       FilenameFilter filter,
                                       boolean recursive)
                                throws IOException
Copy a directory.

Parameters:
source - The directory to copy.
dest - The destination to copy it to.
filter - The filename filter to apply. A value of null results in the entire contents being copied.
recursive - Specifies whether the directory should be copied recursively.
Throws:
IOException

copyDirectory

public static final void copyDirectory(File source,
                                       File dest,
                                       FilenameFilter filter)
                                throws IOException
Recursively copy filtered contents of a directory.

Parameters:
source - The directory to copy.
dest - The destination to copy it to.
filter - The filename filter to apply. A value of null results in the entire contents being copied.
Throws:
IOException

copyDirectory

public static final void copyDirectory(File source,
                                       File dest)
                                throws IOException
Recursively copy a directory.

Parameters:
source - The directory to copy.
dest - The destination to copy it to.
Throws:
IOException

deleteDirectory

public static final void deleteDirectory(File dir)
                                  throws IOException
Throws:
IOException

readableFileSize

public static String readableFileSize(long size)

relativePath

public static String relativePath(File directory,
                                  File file)
Gets the path to the given file relative to the given directory.

Parameters:
directory - The directory.
file - The file
Returns:
The path of the file relative to the directory.

getUserHome

public static final String getUserHome()
Returns the current user's home directory.

Returns:
The user's home directory or null, if it none exists.

getUserHomeDir

public static final File getUserHomeDir()
Returns the current user's home directory.

Returns:
The user's home directory or null, if it none exists.

expandPath

public static String expandPath(String path)
Expands the given String path with any path variables such as user home directory (e.g. ~/myDir) might be expanded to /users/root/myDir

Parameters:
path - The path to be expanded.
Returns:
The expanded path, possibly the same as that passed in if no expansion was done.

readFile

public static final String readFile(File file,
                                    String charset)
                             throws IOException
Reads a file into a String.

Parameters:
file - The file
charset - The character set to use for file content
Returns:
The file contents as a String.
Throws:
IOException - If there is an error reading the file

newFileWithNamespace

public static final File newFileWithNamespace(File parent,
                                              String namespace,
                                              String name,
                                              String extension)
Convenience helper for constructing File instances that involve java-style namespaces as their segment.

Parameters:
parent - Parent directory
namespace - dot-separated namespace component that is converted to filesystem path segment prepended to file.
name - File name.
extension - Filename extension excluding dot character.
Returns:
File instance.

newFileWithNamespace

public static final File newFileWithNamespace(File parent,
                                              String namespace,
                                              String nameWithExtension)
Equivalent to calling newFileWithNamespace(File, String, String, String) with extension = null.

Parameters:
parent - Parent directory
namespace - dot-separated namespace component that is converted to filesystem path segment prepended to file.
nameWithExtension - File name with extension included.
Returns:
File instance.

newDirWithNamespace

public static final File newDirWithNamespace(File parent,
                                             String namespace)
Creates File instance of directory with dot-separated namespace appended to the path.

Parameters:
parent - Parent directory.
namespace - Namespace to append to directory path.
Returns:
File instance of directory with namespace.

findFilesByEnding

public static final Collection<File> findFilesByEnding(File dir,
                                                       String ending)
Searches a directory recursively to find files that match given ending, ignoring case. Note that this method does not handle file system links and has no limit on number of results and depth.

Parameters:
dir - Directory to visit recursively.
ending - Extension to match, excluding the preceding dot.
Returns:
Collection of found files matching the search criteria.


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.