public class FileLib
extends java.lang.Object
In general, you should use the VFS framework in com.partnersoft.io.vfs for all file and directory operations. VFS provides a better API and better implementation of many file-related methods, and is also applicable to file-like things like .zip files.
If you must use java.io.File for some reason, you should use these instead of calling the corresponding functions on java.io.File objects, or rolling your own versions. Many of them are fiddly. For example, in Windows environments, file locking is a huge problem and delete() cannot be assumed to work. Recursive methods for deleting file trees are also problematic and you shouldn't roll your own.
Copyright 2003-2012 Partner Softare, Inc.
| Modifier and Type | Field and Description |
|---|---|
static int |
BUFFER_SIZE |
static java.text.SimpleDateFormat |
timestampFormat |
| Constructor and Description |
|---|
FileLib() |
| Modifier and Type | Method and Description |
|---|---|
static java.io.File |
canonicalize(java.io.File victim) |
static long |
checksum(java.io.File victim)
Generates a checksum for the given file.
|
static long |
copy(java.io.File source,
java.io.File dest) |
static long |
copy(java.io.File source,
java.io.File dest,
FileActivityListener nosy) |
static long |
copy(java.lang.String source,
java.lang.String dest) |
static void |
copyTree(java.io.File source,
java.io.File destinationDir)
Copies an entire directory tree.
|
static void |
copyTree(java.io.File source,
java.io.File destinationDir,
FileActivityListener nosy)
Copies an entire directory tree.
|
static void |
copyTreeContents(java.io.File source,
java.io.File destinationDir)
Copies an entire directory tree.
|
static void |
copyTreeContents(java.io.File source,
java.io.File destinationDir,
FileActivityListener nosy) |
static void |
copyTreeNoOverwrite(java.io.File source,
java.io.File destination)
this will copy two trees making sure not to overwrite any exsisting info
|
static int |
countFilesInTree(java.io.File dir) |
static java.io.File |
createDir(java.io.File dir,
java.lang.String filename)
Creates a File object for a subdirectory of an existing directory,
unscrubbing the name and making the directory if necessary.
|
static java.io.File |
createDir(java.lang.String filename)
Creates a File object for a directory, unscrubbing the string and making
the directory if needed.
|
static java.io.File |
createFile(java.io.File dir,
java.lang.String filename)
Creates a File object for an existing directory, unscrubbing the name.
|
static java.io.File |
createFile(java.lang.String filename)
Creates a File object, unscrubbing the string.
|
static java.io.File |
createFileMakeDir(java.io.File dir,
java.lang.String filename)
Creates a File object for an existing directory, unscrubbing the name and
making the parent directory if necessary.
|
static java.io.File |
createFileMakeDir(java.lang.String filename)
Creates a File object, unscrubbing the string and making the parent if
necessary.
|
static java.io.File |
createTempDir()
Tries to create a temporary directory by creating a temporary file, then
deleting it and making a directory.
|
static java.io.File |
createTempFile(java.io.File file)
Creates a tempfile for the given file, by appending ".temp" to its name.
|
static java.io.File |
createTempFile(java.lang.String filename)
Creates a tempfile for the given file, by appending ".temp" to its name.
|
static boolean |
deleteContents(java.io.File dir)
Deletes contents of dir.
|
static void |
deleteFileAndParentIfEmpty(java.io.File victim)
Deletes the given file.
|
static void |
deleteTree(java.io.File file)
Deprecated.
use removeFile() or removeDirectory().
|
static void |
eject(java.io.File root) |
static long |
fileCopy(java.io.File source,
java.io.File dest)
Binary copies a file from source to destination.
|
static java.io.File |
getInstallRoot()
Root directory of Partner install.
|
static boolean |
isBeneath(java.io.File filet,
java.io.File root)
Returns true if the given file is the root or a file "beneath" (a
subdirectory of or in a subdirectory of) the root.
|
static boolean |
isInsideInstall(java.io.File filet)
Returns true if the given file is the root or a file "beneath" (a
subdirectory of or in a subdirectory of) the root.
|
static boolean |
isSubpath(java.lang.String path)
Returns true if the file path is a sub path - i.e., no relative parent
references or absolute references.
|
static java.lang.String[] |
listFilesRemoveExtension(java.io.File dir,
java.lang.String extension)
Returns a list of files with the given extension, minus the extension.
|
static byte[] |
loadFile(java.io.File source)
Binary copies a file from source into a byte array in memory.
|
static java.lang.String |
loadFileAsString(java.io.File file)
Slurps a text file in as a single big String.
|
static long[] |
loadFileOfLongs(java.io.File source)
Binary copies a file from source into a long array in memory.
|
static byte[] |
loadStream(java.io.InputStream source)
Identical to loadFile().
|
static void |
makeDirsFor(java.io.File file)
Creates parent directories for a file object.
|
static java.lang.String |
makeValidFilename(java.lang.String source) |
static void |
mount(java.io.File root)
Attempt to mount a filesystem.
|
static void |
move(java.io.File from,
java.io.File to) |
static java.io.File |
moveToTempFile(java.io.File original)
Moves a file to a tempfile, ensuring deletion happens etc.
|
static java.lang.String |
relativeOrAbsolutePathFor(java.io.File filet) |
static java.lang.String |
relativeOrAbsolutePathFor(java.io.File root,
java.io.File filet)
Returns the path relative to some root.
|
static java.lang.String |
relativePath(java.io.File root,
java.io.File filet)
Returns the path relative to some root.
|
static boolean |
removeDirectory(java.io.File file)
If possible, uses OS-native tools to remove the given directory and its entire contents tree.
|
static java.lang.String |
removeExtension(java.lang.String filename)
Returns everything in the string up to the last period (.); handy for
chopping off extensions.
|
static boolean |
removeFile(java.io.File file)
Does various OS-specific things to properly remove the given File.
|
static java.lang.String |
removeFileName(java.lang.String filename)
Just returns the directory path (not the filename).
|
static java.lang.String |
removeParentPath(java.lang.String parent,
java.lang.String victim)
Removes a parent path from a full path.
|
static java.lang.String |
removePathAndExtension(java.lang.String filename)
Returns everything in the string up to the last period (.); handy for
chopping off extensions.
|
static void |
removeTree(java.io.File victim)
Deprecated.
use removeFile() or removeDirectory().
|
static void |
removeTree(java.io.File victim,
FileActivityListener nosy)
Deprecated.
use removeFile() or removeDirectory()
|
static void |
removeTreeContents(java.io.File victim)
Removes the contents of an entire directory tree.
|
static java.io.File |
reserveTempFile(java.io.File original)
Creates a tempfile for the given file, by appending ".temp" to its name;
deletes any existing.
|
static void |
saveFile(byte[] contents,
java.io.File source)
Binary copies a byte array in memory to a file.
|
static void |
saveFile(java.lang.String contents,
java.io.File source)
Saves a string to a file.
|
static java.lang.String |
separatorToSlash(java.lang.String filename)
Scrubs a string, turning native File.separator strings into Unix-style
slashes.
|
static java.lang.String |
slashToSeparator(java.lang.String filename)
Unscrubs a string, turning Unix-style slashes into native File.separator
strings.
|
static java.lang.String |
timestampFileName(java.lang.String filename)
Adds a date and timestamp to the filename, just before the extension.
|
static void |
unmount(java.io.File root)
Attempt to unmount a filesystem.
|
public static final int BUFFER_SIZE
public static java.text.SimpleDateFormat timestampFormat
public static java.io.File getInstallRoot()
public static boolean isInsideInstall(java.io.File filet)
throws java.io.IOException
java.io.IOExceptionpublic static boolean isBeneath(java.io.File filet,
java.io.File root)
public static void mount(java.io.File root)
throws java.io.IOException
java.io.IOExceptionpublic static void unmount(java.io.File root)
throws java.io.IOException
java.io.IOExceptionpublic static void eject(java.io.File root)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String slashToSeparator(java.lang.String filename)
public static java.lang.String separatorToSlash(java.lang.String filename)
public static boolean isSubpath(java.lang.String path)
public static java.io.File createFile(java.lang.String filename)
public static java.io.File createFile(java.io.File dir,
java.lang.String filename)
public static java.io.File createFileMakeDir(java.lang.String filename)
throws java.io.IOException
java.io.IOExceptionpublic static void makeDirsFor(java.io.File file)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.File createFileMakeDir(java.io.File dir,
java.lang.String filename)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.File createDir(java.lang.String filename)
public static java.io.File createDir(java.io.File dir,
java.lang.String filename)
public static boolean deleteContents(java.io.File dir)
throws java.io.IOException
java.io.IOExceptionpublic static void deleteTree(java.io.File file)
throws java.io.IOException
file - java.io.IOExceptionpublic static void copyTree(java.io.File source,
java.io.File destinationDir)
throws java.io.IOException
java.io.IOExceptionpublic static void copyTree(java.io.File source,
java.io.File destinationDir,
FileActivityListener nosy)
throws java.io.IOException
java.io.IOExceptionpublic static void copyTreeContents(java.io.File source,
java.io.File destinationDir)
throws java.io.IOException
java.io.IOExceptionpublic static void copyTreeContents(java.io.File source,
java.io.File destinationDir,
FileActivityListener nosy)
throws java.io.IOException
java.io.IOExceptionpublic static void copyTreeNoOverwrite(java.io.File source,
java.io.File destination)
throws java.io.IOException
java.io.IOExceptionpublic static void removeTree(java.io.File victim)
throws java.io.IOException
java.io.IOExceptionpublic static void removeTree(java.io.File victim,
FileActivityListener nosy)
throws java.io.IOException
victim - nosy - java.io.IOExceptionpublic static void removeTreeContents(java.io.File victim)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String removeParentPath(java.lang.String parent,
java.lang.String victim)
throws java.io.IOException
java.io.IOExceptionpublic static int countFilesInTree(java.io.File dir)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.File source,
java.io.File dest)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.File source,
java.io.File dest,
FileActivityListener nosy)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.lang.String source,
java.lang.String dest)
throws java.io.IOException
java.io.IOExceptionpublic static long fileCopy(java.io.File source,
java.io.File dest)
throws java.io.IOException
java.io.IOExceptionpublic static byte[] loadStream(java.io.InputStream source)
throws java.io.IOException
java.io.IOExceptionpublic static byte[] loadFile(java.io.File source)
throws java.io.IOException
java.io.IOExceptionpublic static long[] loadFileOfLongs(java.io.File source)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String loadFileAsString(java.io.File file)
throws java.io.IOException
java.io.IOExceptionpublic static void saveFile(byte[] contents,
java.io.File source)
throws java.io.IOException
java.io.IOExceptionpublic static void saveFile(java.lang.String contents,
java.io.File source)
throws java.io.IOException
java.io.IOExceptionpublic static long checksum(java.io.File victim)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String[] listFilesRemoveExtension(java.io.File dir,
java.lang.String extension)
public static java.lang.String removeExtension(java.lang.String filename)
public static java.lang.String removePathAndExtension(java.lang.String filename)
public static java.lang.String removeFileName(java.lang.String filename)
public static java.lang.String timestampFileName(java.lang.String filename)
public static java.io.File createTempFile(java.lang.String filename)
public static java.io.File reserveTempFile(java.io.File original)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.File moveToTempFile(java.io.File original)
throws java.io.IOException
java.io.IOExceptionpublic static void move(java.io.File from,
java.io.File to)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.File createTempFile(java.io.File file)
public static java.io.File createTempDir()
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String relativePath(java.io.File root,
java.io.File filet)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String relativeOrAbsolutePathFor(java.io.File root,
java.io.File filet)
public static java.lang.String relativeOrAbsolutePathFor(java.io.File filet)
public static void deleteFileAndParentIfEmpty(java.io.File victim)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.File canonicalize(java.io.File victim)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String makeValidFilename(java.lang.String source)
public static boolean removeFile(java.io.File file)
throws java.io.IOException
java.io.IOExceptionpublic static boolean removeDirectory(java.io.File file)
throws java.io.IOException
java.io.IOException