Class File
java.io.File making it almost into a "drop in" replacement.
It is placed in a different package because it is incompatible to java.io.File by definition. It is useful
in getting some simple code to work without too many changes-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for filtering files.static interfaceInterface to filter filenames. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new file in the given parent directory, and subpath.Creates a new file object with given path.Creates a new file object in a given directory.Creates a new File object from the given URI -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if this file is executable.booleanCreates this file as a new blank file in the file system.static FilecreateTempFile(String prefix, String suffix) Creates a temporary file.booleandelete()Deletes the file described by this object on the file system.booleanChecks if the given object refers to the same file.booleanexists()Checks if the file described by this object exists on the file system.Gets the absolute file - which is always itself, since#isAbsolute()always returns true.Gets the absolute path of the file as a string,longGets the free space on the root file system.getName()Returns the file name.Gets the parent directory path.Returns the file object for the parent directory.getPath()Gets the path to the file.longReturns the total space on the root file system.longGets the usable space on this file system.inthashCode()Returns a hash code value for the object.booleanChecks if the path is absolute.booleanChecks if this file is a directory.booleanisFile()Checks if this file object represents a regular file.booleanisHidden()Checks if this is a hidden file.longGets the last modified time as a unix timestamp in milliseconds.longlength()Gets the file size in bytes.String[]list()Returns the list of child files of this directory.String[]list(File.FilenameFilter filter) Returns list of child files of this directoryFile[]Gets a list of child files of this directory.File[]Gets a list of child files of this directory, filtering them using the provided filter.File[]Gets a list of child files of this directory, filtered using the provided filter.static File[]List the file system roots.booleanmkdir()Attempts to make the directory described by this object.booleanmkdirs()Attempts to make the directory (and all parent directories) of this object.booleanRenames the file to the provided file object.toString()Returns a string representation of the object.toURI()Converts this file to a URI.toURL()Converts this file to a URL.
-
Field Details
-
separatorChar
public static final char separatorChar- See Also:
-
separator
- See Also:
-
-
Constructor Details
-
File
Creates a new File object from the given URI
Parameters
uri
-
File
Creates a new file object with given path. Paths that do not begin with the "file:" prefix will automatically be prefixed with the app home path.
Parameters
path: The path of the file. Relative or absolute.
-
File
-
File
-
-
Method Details
-
listRoots
List the file system roots. -
createTempFile
Creates a temporary file.
Parameters
-
prefix: The file name prefix. -
suffix: The file name suffix
Returns
The resulting temporary file.
Throws
IOException
- Throws:
IOException
-
-
getName
Returns the file name.
Returns
The file name.
-
getParent
Gets the parent directory path.
Returns
The parent directory path.
-
getParentFile
Returns the file object for the parent directory. -
getPath
Gets the path to the file. -
isAbsolute
public boolean isAbsolute()Checks if the path is absolute. This always returns true as all File objects use absolute paths - even if they were created with relative paths. Relative paths are automatically prefixed with the app home directory path. -
getAbsolutePath
Gets the absolute path of the file as a string, -
getAbsoluteFile
Gets the absolute file - which is always itself, since
#isAbsolute()always returns true.Returns
The same file object.
-
exists
public boolean exists()Checks if the file described by this object exists on the file system. -
isDirectory
public boolean isDirectory()Checks if this file is a directory. -
isFile
public boolean isFile()Checks if this file object represents a regular file. -
isHidden
public boolean isHidden()Checks if this is a hidden file. -
lastModified
public long lastModified()Gets the last modified time as a unix timestamp in milliseconds. -
length
public long length()Gets the file size in bytes.
Returns
The file size in bytes.
-
createNewFile
Creates this file as a new blank file in the file system.
Returns
True if it succeeds.
Throws
IOException
- Throws:
IOException
-
delete
public boolean delete()Deletes the file described by this object on the file system.
Returns
True if delete succeeds.
-
list
Returns the list of child files of this directory. -
list
Returns list of child files of this directory
Parameters
filter
-
listFiles
Gets a list of child files of this directory. -
listFiles
Gets a list of child files of this directory, filtered using the provided filter.
Parameters
ff: The filter to use.
-
listFiles
Gets a list of child files of this directory, filtering them using the provided filter.
Parameters
ff: The filter to use to filter output.
-
mkdir
public boolean mkdir()Attempts to make the directory described by this object.
Returns
True on success.
-
mkdirs
public boolean mkdirs()Attempts to make the directory (and all parent directories) of this object.
Returns
True on success.
-
renameTo
Renames the file to the provided file object.
Parameters
f: The file object that we are renaming the file to.
Returns
True on success.
-
canExecute
public boolean canExecute()Checks if this file is executable. -
getTotalSpace
public long getTotalSpace()Returns the total space on the root file system. -
getFreeSpace
public long getFreeSpace()Gets the free space on the root file system. -
getUsableSpace
public long getUsableSpace()Gets the usable space on this file system. -
equals
-
hashCode
public int hashCode()Description copied from class:ObjectReturns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode()) -
toURL
Converts this file to a URL.
Throws
MalformedURLException
- Throws:
MalformedURLException
-
toURI
Converts this file to a URI.
Throws
URISyntaxException
- Throws:
URISyntaxException
-