|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.infinityloop.util.inout.DiskFile
public class DiskFile
This class implements a platform neutral container for resource locations, which can be URLs or local file/path specifiers, both relative and absolute, along with the necessary conversion methods to convert from one to the other. It handles all the name encoding, path construction and root volume problems java.io.File has, as well as platform issues.
Also, we can determine whether the 'file' is a folder, whether it is relative, and can calculate the relative path from two DiskFile's.
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
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 |
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. |
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 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 |
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 |
exists()
Checks for real existance of this DiskFile. |
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. |
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.io.File |
getFile()
Returns the File equivalent (if possible) of this object. |
java.lang.String |
getGAuthority()
|
java.lang.String |
getGDevice()
|
java.lang.String |
getGProtocol()
|
java.lang.String |
getGQuery()
|
java.lang.String |
getGRawPath()
|
java.io.InputStream |
getInputStream(boolean doThrow)
Gets an InputStream to this DiskFile. |
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. |
boolean |
isAbsolute()
Determines if this object represents an absolute path. |
boolean |
isFolder()
Returns whether this object represents a file or a folder. |
boolean |
isRelative()
Determines if this object represents a relative path. |
boolean |
isVisible()
Checks for visibility of a DiskFile. |
static void |
main(java.lang.String[] args)
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. |
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 boolean |
moveFile(java.lang.String srcfile,
java.lang.String destdir)
Moves the file from its original location with the same name to the directory specified. |
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()
Reads an ASCII file into memory and returns the contents as a StringBuffer. |
java.lang.StringBuffer |
readToStringBuffer(java.lang.String encoding)
Reads a file into memory and returns the contents as a StringBuffer. |
java.util.Stack |
resolveToNameSequence()
Resolves the passed file name into a name sequence, splitting at '/' or '\' characters. |
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 |
writeString(java.lang.String data,
java.lang.String encoding,
boolean append)
Write a string to a file, optionally appending. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
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 folderMethod Detail |
---|
public 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 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 used
public 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 getGQuery()
public final java.lang.String getGRawPath()
public boolean ensurePath()
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 null
java.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 null
java.lang.Exception
public boolean exists()
public boolean isVisible()
public java.lang.StringBuffer readToStringBuffer()
public java.lang.StringBuffer readToStringBuffer(java.lang.String encoding)
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 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 boolean moveFile(java.lang.String srcfile, java.lang.String destdir)
This is done by first copying the file there, then deleting the original.
srcfile
- the source file to movedestdir
- the directory where it should be moved to
public static boolean copyFile(java.io.File src, java.io.File dest)
src
- the source file to movedest
- the directory where it should be moved to
public 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 string
public static java.lang.String getPathPrefix(java.io.File f)
f
- the absolute path
public 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 fly
public 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 used
public static DiskFile ensureNotExistingFilename(DiskFile target)
.ext
) until the
resulting file does not yet exist.
target
- the desired, absolute target name to be unique
public java.lang.String getExtension()
public boolean delete()
public 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 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 exists
java.lang.Exception
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |