public class DiskFile
extends java.lang.Object
Also, we can determine whether the 'file' is a folder, whether it is relative, and can calculate the relative path from two DiskFile's.
Modifier and Type | Class and Description |
---|---|
static class |
DiskFile.FileProperty |
static class |
DiskFile.PathComparisonMode |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
kLocationParamPrefix
File start location key prefix
|
Constructor and Description |
---|
DiskFile(java.io.File file)
Constructs from a java.io.File.
|
DiskFile(java.lang.String s)
Constructs from a String.
|
DiskFile(java.lang.String s,
boolean folder)
Construct from a String and additionally explicitly specify whether it's a file or folder.
|
Modifier and Type | Method and Description |
---|---|
int |
binaryCompareTo(DiskFile file)
Do a binary compare of this file to the passed file.
|
static DiskFile |
calcAbsoluteDestfile(java.lang.String src,
java.lang.String ext,
java.lang.String destDirectory,
boolean createIntermediateDirs)
Calculates the absolute file name from various given components.
|
static java.lang.String |
calcAbsolutePath(java.lang.String basePath,
java.lang.String relativePath)
Helper: returns the absolute path for the relative one passed in, based on the absolute base path also specified.
|
static java.lang.String |
calcRelativePathNative(java.lang.String src,
java.lang.String dest,
java.lang.String upHierarchyString)
Helper: returns the relative path from the source directory to the destination directory.
|
static java.lang.String |
calcRelativePathToFileURL(java.lang.String startfolder,
java.lang.String destfile,
java.lang.String upHierarchyString)
Calculate the relative path (if possible) from the startfolder to the specified destfile.
|
static java.lang.String |
calcRelativePathURL(java.lang.String src,
java.lang.String dest,
java.lang.String upHierarchyString)
Helper: returns the relative path from the source directory to the destination directory in URL format.
|
boolean |
copy(DiskFile dest)
Copy the contents of this file to the given destination
|
static boolean |
copyFile(java.io.File src,
java.io.File dest)
Copies the file from its original location with the same name to the directory specified.
|
static boolean |
copyFile(java.io.InputStream inStream,
java.io.File dest)
Overloaded version of
copyFile(File,File) , only that the source is an InputStream . |
static boolean |
copyFiles(java.io.File src,
java.io.File dest,
FileProcessor fp)
Recursively copy all files from one directory to another.
|
static boolean |
copyFiles(java.io.File src,
java.io.FilenameFilter filter,
java.io.File dest,
boolean sorted,
boolean overwrite,
FileProcessor fp)
Copy all files from one folder to the other (recursively)
|
boolean |
create(boolean overWriteExisting)
Create the object this DiskFile presents, optionally replacing an object of the same name that might already exist.
|
boolean |
createSymbolicLink(DiskFile link)
Creates a symbolic link under the specified path to this file object
|
static DiskFile |
createTemporaryFile(java.lang.String destPath,
java.lang.String extension)
Creates a DiskFile object for a temporary, non-existing file.
|
boolean |
delete()
Delete this DiskFile.
|
boolean |
deleteContentsRecursively()
Delete this DiskFile's content.
|
boolean |
deleteRecursively(java.lang.String rootDir)
Delete this DiskFile.
|
static DiskFile |
ensureNotExistingFilename(DiskFile target)
This method ensures that the passed file name does not yet exist.
|
boolean |
ensurePath()
Ensures that the given path actually does exist, creating intermediate dirs if necessary.
|
boolean |
equals(DiskFile otherFile)
Checks if this DiskFile specifies the same file as the passed one.
|
boolean |
exists()
Checks for real existance of this DiskFile.
|
boolean |
exists(DiskFile.PathComparisonMode mode) |
java.lang.String |
getBaseName()
retrieves only the basename (i.e. its name without path, extension and trailing dot) of the specified file (if it is a file), the name otherwise.
|
java.lang.String |
getBaseNameURL()
retrieves only the basename (i.e. its name without path, extension and trailing dot) of the specified file (if it is a file), the name otherwise.
|
byte[] |
getBytes()
Reads a file into memory and returns the contents as a a bytes array.
|
java.lang.String |
getDevice() |
java.lang.String |
getExtension()
Returns the extension (excl. the dot) of the file or the empty string, if the file does not have an extension.
|
java.lang.String |
getExtensionURL()
Returns the extension (excl. the dot) of the file or the empty string, if the file does not have an extension, in URL format
|
java.io.File |
getFile()
Returns the File equivalent (if possible) of this object.
|
java.lang.String |
getGAuthority() |
java.lang.String |
getGDevice() |
java.lang.String |
getGFragmentRaw() |
java.lang.String |
getGProtocol() |
java.lang.String |
getGQueryRaw() |
java.lang.String |
getGRawPath() |
java.io.InputStream |
getInputStream(boolean doThrow)
Gets an InputStream to this DiskFile.
|
java.lang.String |
getJarFile() |
java.lang.String |
getName()
retrieves only the name of the specified file.
|
java.lang.String |
getNameURLEncoded()
retrieves only the name of the specified file, encoded for use in a URL.
|
java.io.OutputStream |
getOutputStream(boolean doThrow)
Gets resp. creates an OutputStream associated with this DiskFile.
|
DiskFile |
getPathOnly()
retrieves the path to the specified file (if it is a file), otherwise returns unaltered.
|
static java.lang.String |
getPathPrefix(java.io.File f)
Retrieves the path prefix (which does not include any leading platform specific drive specifier like C:\) from an absolute file name.
|
java.util.Map<java.lang.String,java.lang.Object> |
getProperties()
Return a map of properties for this object
|
boolean |
hide()
Set the "hidden" flag on the file (if on Windows)
|
boolean |
isAbsolute()
Determines if this object represents an absolute path.
|
boolean |
isDescendantOf(DiskFile ancestor)
This function returns true if this file system object resides in the subtree below the file system object passed (meaning this function returns
false always when the passed one is not a folder, but a file, which by definition is a leaf and cannot have descendants).
|
boolean |
isFolder()
Returns whether this object represents a file or a folder.
|
boolean |
isReadable()
Check if the object this DiskFile points to (which may be a network object or a file in a jar) is readable
|
boolean |
isRelative()
Determines if this object represents a relative path.
|
boolean |
isURL() |
boolean |
isVisible()
Checks for visibility of a DiskFile.
|
static void |
main(java.lang.String[] args) |
DiskFile |
makeAbsolute(java.lang.String basePath)
Returns the File equivalent (if possible) of this relative object.
|
static java.lang.String |
makePathString(java.lang.String path)
Makes a path string from the passed string, i.e. it appends File.separator to it if that's not the last
character in the string.
|
static java.lang.String |
md5(DiskFile df)
Returns the MD5 Hash of the specified file as Hex String.
|
static java.lang.String |
md5(java.lang.String file)
Returns the MD5 Hash of the specified file as Hex String.
|
static boolean |
moveFile(DiskFile srcfile,
DiskFile destFileOrFolder)
Moves the file from its original location with the same name to the directory specified.
|
static boolean |
moveFolder(DiskFile srcfolder,
DiskFile destFolder)
Moves the file from its original location with the same name to the directory specified.
|
static java.lang.String |
parseURLintoXML(java.lang.String url)
Parses and breaks up the passed URL into its components and returns it in XMLified form so that after (re)parsing that XML
code, it can easily be acessed e.g. with XSLT.
|
static DiskFile |
PickFileOpen(java.awt.Frame parent,
java.lang.String titleText,
java.lang.Object defaultLocation,
java.util.Vector fileFilters,
de.infinityloop.util.Preferences prefsObject)
Picks a file to be opened.
|
static DiskFile |
PickFileOrFolderOpen(java.awt.Frame parent,
java.lang.String titleText,
java.lang.Object defaultLocation,
java.util.Vector fileFilters,
de.infinityloop.util.Preferences prefsObject)
Picks a file or folder to be opened.
|
static DiskFile |
PickFileSave(java.awt.Frame parent,
java.lang.String titleText,
java.lang.Object defaultLocation,
java.util.Vector fileFilters,
de.infinityloop.util.Preferences prefsObject)
Picks a file to be saved.
|
static DiskFile |
PickFolder(java.awt.Frame parent,
java.lang.String titleText,
java.lang.Object defaultLocation,
java.util.Vector fileFilters,
de.infinityloop.util.Preferences prefsObject)
Picks a folder
|
java.lang.StringBuffer |
readToStringBuffer(java.lang.String encoding)
Reads a file into memory and returns the contents as a StringBuilder.
|
java.util.Stack<java.lang.String> |
resolveToNameSequence()
Resolves the passed file name into a name sequence, splitting at '/' or '\' characters.
|
void |
setExtension(java.lang.String extension)
Sets (i.e., adds or changes the existing) the specified extension on this object.
|
java.lang.String |
toLocalPathString()
Retrieves the absolute path to the file designated by this object in local platform naming convention.
|
java.lang.String |
toLocalPathStringNoTrailingSeparator() |
java.lang.String |
toString()
For debugging
|
java.lang.String |
toURLString()
makes a file: or other protocol URL equivalent from this object as String.
|
java.lang.String |
toURLStringNoTrailingSeparator() |
void |
writeData(byte[] data,
boolean append)
Write a byte array to a file, optionally appending.
|
void |
writeString(java.lang.String data,
java.lang.String encoding,
boolean append)
Write a string to a file, optionally appending.
|
public static final java.lang.String kLocationParamPrefix
public DiskFile(java.io.File file)
file
- File objectpublic DiskFile(java.lang.String s)
s
- the String identifying the filepublic DiskFile(java.lang.String s, boolean folder)
s
- the Stringfolder
- true if s designates a folderpublic java.lang.String getDevice()
public java.lang.String toLocalPathString()
public java.lang.String toLocalPathStringNoTrailingSeparator()
public java.lang.String toURLString()
public java.lang.String toURLStringNoTrailingSeparator()
public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
public java.util.Stack<java.lang.String> resolveToNameSequence()
public boolean isFolder()
public java.io.File getFile()
public DiskFile makeAbsolute(java.lang.String basePath)
basePath
- the base path to which this object should be considered relative to; when null, the running user's
current directory is usedpublic DiskFile getPathOnly()
public java.lang.String getName()
public java.lang.String getNameURLEncoded()
public java.lang.String getBaseName()
public java.lang.String getBaseNameURL()
public final java.lang.String getGAuthority()
public final java.lang.String getGDevice()
public final java.lang.String getGProtocol()
public final java.lang.String getGQueryRaw()
public final java.lang.String getGRawPath()
public final java.lang.String getJarFile()
public final boolean isURL()
public boolean isAbsolute()
public boolean isRelative()
public java.io.InputStream getInputStream(boolean doThrow) throws java.lang.Exception
doThrow
- if true, this method throws on an error, otherwise it returns nulljava.lang.Exception
public java.io.OutputStream getOutputStream(boolean doThrow) throws java.lang.Exception
doThrow
- if true, this method throws on an error, otherwise it returns nulljava.lang.Exception
public boolean ensurePath()
public boolean create(boolean overWriteExisting)
overWriteExisting
- when true, an object of the same name is deleted.public boolean isReadable()
public boolean exists()
public boolean exists(DiskFile.PathComparisonMode mode)
public boolean isVisible()
public boolean hide()
public java.lang.StringBuffer readToStringBuffer(java.lang.String encoding)
public byte[] getBytes()
public static java.lang.String calcAbsolutePath(java.lang.String basePath, java.lang.String relativePath) throws java.io.IOException
basePath
- the basepath where relativePath should be interpreted relative torelativePath
- the relative path to make absolute with respect to the base path. If it is already absolute, it is returned unaltered.java.io.IOException
public static java.lang.String calcRelativePathToFileURL(java.lang.String startfolder, java.lang.String destfile, java.lang.String upHierarchyString)
startfolder
- destfile
- upHierarchyString
- public static java.lang.String calcRelativePathURL(java.lang.String src, java.lang.String dest, java.lang.String upHierarchyString)
src
- the path where we start travellingdest
- the path where we want to beupHierarchyString
- String in relative path that designates "up one hierarchy". Normally,
this is "..". The path separator character is automatically appended, so it gets e.g. "../"public static java.lang.String calcRelativePathNative(java.lang.String src, java.lang.String dest, java.lang.String upHierarchyString)
src
- the path where we start travellingdest
- the path where we want to beupHierarchyString
- String in relative path that designates "up one hierarchy". Normally,
this is "..". The path separator character is automatically appended, so it gets e.g. "../"public static java.lang.String md5(DiskFile df) throws java.lang.Exception
df
- the DiskFile to calculate the hash on. Must be an existing, readable filejava.lang.Exception
public static java.lang.String md5(java.lang.String file) throws java.lang.Exception
file
- the absolute file pathjava.lang.Exception
public int binaryCompareTo(DiskFile file) throws java.lang.Exception
file
- the absolute file path to the file to compare this file tojava.lang.Exception
public static boolean moveFile(DiskFile srcfile, DiskFile destFileOrFolder)
This is done by first copying the file there, then deleting the original.
srcfile
- the source file to movedestFileOrFolder
- either the absolute file name to the destination (with possibly being renamed), or just the directory where it should be moved to (keeping its original file name)public static boolean moveFolder(DiskFile srcfolder, DiskFile destFolder)
This is done by first copying the file there, then deleting the original.
srcfolder
- the source file to movedestFolder
- either the absolute file name to the destination (with possibly being renamed), or just the directory where it should be moved to (keeping its original file name)public boolean equals(DiskFile otherFile)
otherFile
- the file spec to compare topublic static boolean copyFile(java.io.File src, java.io.File dest)
src
- the source file to movedest
- the directory where it should be moved topublic static boolean copyFile(java.io.InputStream inStream, java.io.File dest)
copyFile(File,File)
, only that the source is an InputStream
.public static java.lang.String makePathString(java.lang.String path)
path
- the String to make a path stringpublic static java.lang.String getPathPrefix(java.io.File f)
f
- the absolute pathpublic static DiskFile PickFileOpen(java.awt.Frame parent, java.lang.String titleText, java.lang.Object defaultLocation, java.util.Vector fileFilters, de.infinityloop.util.Preferences prefsObject)
parent
- the parent frame the open dialog hastitleText
- the dialog's titledefaultLocation
- if a String object, it is interpreted to mean a key which is used to lookup the last location
for this operation; if a DiskFile object it means that specific location; if null, we use user.home by default.fileFilters
- should contain a Vector of objects that are javax.swing.filechooser.FileFilter objects, implementing also(!) java.io.FilenameFilter.public static DiskFile PickFileOrFolderOpen(java.awt.Frame parent, java.lang.String titleText, java.lang.Object defaultLocation, java.util.Vector fileFilters, de.infinityloop.util.Preferences prefsObject)
parent
- the parent frame the open dialog hastitleText
- the dialog's titledefaultLocation
- if a String object, it is interpreted to mean a key which is used to lookup the last location
for this operation; if a DiskFile object it means that specific location; if null, we use user.home by default.fileFilters
- should contain a Vector of objects that are javax.swing.filechooser.FileFilter objects, implementing also(!) java.io.FilenameFilter.public static DiskFile PickFileSave(java.awt.Frame parent, java.lang.String titleText, java.lang.Object defaultLocation, java.util.Vector fileFilters, de.infinityloop.util.Preferences prefsObject)
parent
- the parent frame the open dialog hastitleText
- the dialog's titledefaultLocation
- if a String object, it is interpreted to mean a key which is used to lookup the last location
for this operation; if a DiskFile object it means that specific location; if null, we use user.home by default.fileFilters
- should contain a Vector of objects that are javax.swing.filechooser.FileFilter objects, implementing also(!) java.io.FilenameFilter.public static DiskFile PickFolder(java.awt.Frame parent, java.lang.String titleText, java.lang.Object defaultLocation, java.util.Vector fileFilters, de.infinityloop.util.Preferences prefsObject)
parent
- the parent frame the open dialog hastitleText
- the dialog's titledefaultLocation
- if a String object, it is interpreted to mean a key which is used to lookup the last location
for this operation; if a DiskFile object it means that specific location; if null, we use user.home by default.fileFilters
- should contain a Vector of objects that are javax.swing.filechooser.FileFilter objects, implementing also(!) java.io.FilenameFilter.public static DiskFile calcAbsoluteDestfile(java.lang.String src, java.lang.String ext, java.lang.String destDirectory, boolean createIntermediateDirs)
src
- the source fileext
- the extension to add. When this is exactly one space ' ', the extension of the original file is NOT removed, but left as is.destDirectory
- the directory where the file should wind up. If null, same as source file.createIntermediateDirs
- if true, any missing intermediate directories will be created on the flypublic static DiskFile createTemporaryFile(java.lang.String destPath, java.lang.String extension)
destPath
- The file will be created under this path; if this is null, a system-dependent default path will be used.extension
- the file extension to use (including the dot, e.g. ".rtf"); when null, no extension will be usedpublic static DiskFile ensureNotExistingFilename(DiskFile target)
.ext
) until the
resulting file does not yet exist.target
- the desired, absolute target name to be uniquepublic java.lang.String getExtension()
public java.lang.String getExtensionURL()
public void setExtension(java.lang.String extension)
public boolean delete()
public boolean isDescendantOf(DiskFile ancestor)
ancestor
- the file system object against which we test if this file system object is a descendant ofancestor
, false otherwisepublic boolean deleteRecursively(java.lang.String rootDir)
rootDir
- when non-null, specifies a path under which deletions may only occur (for safety reasons). The
folder specified may NOT be deleted itself.public boolean deleteContentsRecursively()
public boolean copy(DiskFile dest)
dest
- public boolean createSymbolicLink(DiskFile link)
link
- the path under which the link to this DiskFile object should be createdpublic void writeString(java.lang.String data, java.lang.String encoding, boolean append) throws java.lang.Exception
data
- the string to writeencoding
- the encoding to use for writing; when null or the empty string, UTF-8 is usedappend
- when true, data is appended if the file already existsjava.lang.Exception
public void writeData(byte[] data, boolean append) throws java.lang.Exception
data
- the string to writeappend
- when true, data is appended if the file already existsjava.lang.Exception
public static java.lang.String parseURLintoXML(java.lang.String url)
public static boolean copyFiles(java.io.File src, java.io.File dest, FileProcessor fp) throws java.io.IOException
src
- file or directory to copy from.dest
- file or directory to copy to.java.io.IOException
public static boolean copyFiles(java.io.File src, java.io.FilenameFilter filter, java.io.File dest, boolean sorted, boolean overwrite, FileProcessor fp) throws java.io.IOException
src
- source folderfilter
- FilenameFilter to be used on src; use null if no filtering desireddest
- destination foldersorted
- perform copying in sorted orderoverwrite
- when true, overwrite target filejava.io.IOException
public static void main(java.lang.String[] args)
public final java.lang.String getGFragmentRaw()
public java.util.Map<java.lang.String,java.lang.Object> getProperties()