public class File

  1. Object
  2. File
This class provides a similar API to 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 types

interface File.FilenameFilterInterface to filter filenames.
interface File.FileFilterInterface for filtering files.

Fields

public static final char separatorChar = '/'
public static final String separator = "/"

Constructors

public File(URI uri)Creates a new File object from the given URI
public File(String path)Creates a new file object with given path.
public File(String dir, String file)Creates a new file object in a given directory.
public File(File parent, String path)Creates a new file in the given parent directory, and subpath.

Methods

public static File[] listRoots()List the file system roots.
public static File createTempFile(String prefix, String suffix) throws IOExceptionCreates a temporary file.
public String getName()Returns the file name.
public String getParent()Gets the parent directory path.
public File getParentFile()Returns the file object for the parent directory.
public String getPath()Gets the path to the file.
public boolean isAbsolute()Checks if the path is absolute.
public String getAbsolutePath()Gets the absolute path of the file as a string,
public File getAbsoluteFile()Gets the absolute file - which is always itself, since #isAbsolute() always returns true.
public boolean exists()Checks if the file described by this object exists on the file system.
public boolean isDirectory()Checks if this file is a directory.
public boolean isFile()Checks if this file object represents a regular file.
public boolean isHidden()Checks if this is a hidden file.
public long lastModified()Gets the last modified time as a unix timestamp in milliseconds.
public long length()Gets the file size in bytes.
public boolean createNewFile() throws IOExceptionCreates this file as a new blank file in the file system.
public boolean delete()Deletes the file described by this object on the file system.
public String[] list()Returns the list of child files of this directory.
public String[] list(File.FilenameFilter filter)Returns list of child files of this directory
public File[] listFiles()Gets a list of child files of this directory.
public File[] listFiles(File.FilenameFilter ff)Gets a list of child files of this directory, filtered using the provided filter.
public File[] listFiles(File.FileFilter ff)Gets a list of child files of this directory, filtering them using the provided filter.
public boolean mkdir()Attempts to make the directory described by this object.
public boolean mkdirs()Attempts to make the directory (and all parent directories) of this object.
public boolean renameTo(File f)Renames the file to the provided file object.
public boolean canExecute()Checks if this file is executable.
public long getTotalSpace()Returns the total space on the root file system.
public long getFreeSpace()Gets the free space on the root file system.
public long getUsableSpace()Gets the usable space on this file system.
public boolean equals(Object o)Checks if the given object refers to the same file.
public int hashCode()Returns a hash code value for the object.
public String toString()Returns a string representation of the object.
public URL toURL() throws MalformedURLExceptionConverts this file to a URL.
public URI toURI() throws URISyntaxExceptionConverts this file to a URI.

Inherited methods

Field details

separatorChar

public static final char separatorChar = '/'

separator

public static final String separator = "/"

Constructor details

File

public File(URI uri)
Creates a new File object from the given URI

File

public File(String path)
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 String
The path of the file. Relative or absolute.

File

public File(String dir, String file)
Creates a new file object in a given directory.

Parameters

dir String
The parent directory path.
file String
The file name

File

public File(File parent, String path)
Creates a new file in the given parent directory, and subpath.

Parameters

parent File
The parent directory.
path String
The subpath, beginning with the parent directory.

Method details

listRoots

public static File[] listRoots()
List the file system roots.

createTempFile

public static File createTempFile(String prefix, String suffix) throws IOException
Creates a temporary file.

Parameters

prefix String
The file name prefix.
suffix String
The file name suffix

Returns

The resulting temporary file.

getName

public String getName()
Returns the file name.

Returns

The file name.

getParent

public String getParent()
Gets the parent directory path.

Returns

The parent directory path.

getParentFile

public File getParentFile()
Returns the file object for the parent directory.

getPath

public String 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

public String getAbsolutePath()
Gets the absolute path of the file as a string,

getAbsoluteFile

public File 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

public boolean createNewFile() throws IOException
Creates this file as a new blank file in the file system.

Returns

True if it succeeds.

delete

public boolean delete()
Deletes the file described by this object on the file system.

Returns

True if delete succeeds.

list

public String[] list()
Returns the list of child files of this directory.

list

public String[] list(File.FilenameFilter filter)
Returns list of child files of this directory

listFiles

public File[] listFiles()
Gets a list of child files of this directory.

listFiles

public File[] listFiles(File.FilenameFilter ff)
Gets a list of child files of this directory, filtered using the provided filter.

Parameters

ff File.FilenameFilter
The filter to use.

listFiles

public File[] listFiles(File.FileFilter ff)
Gets a list of child files of this directory, filtering them using the provided filter.

Parameters

ff File.FileFilter
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

public boolean renameTo(File f)
Renames the file to the provided file object.

Parameters

f File
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

public boolean equals(Object o)
Checks if the given object refers to the same file.

hashCode

public int hashCode()
Returns 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

public String toString()
Returns 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

public URL toURL() throws MalformedURLException
Converts this file to a URL.

toURI

public URI toURI() throws URISyntaxException
Converts this file to a URI.