File Class

Definition

An abstract representation of file and directory pathnames.

[Android.Runtime.Register("java/io/File", DoNotGenerateAcw=true)]
public class File : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.IComparable
[<Android.Runtime.Register("java/io/File", DoNotGenerateAcw=true)>]
type File = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IComparable
Inheritance
File
Attributes
Implements

Remarks

An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent <em>pathname strings</em> to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An <em>abstract pathname</em> has two components:

<ol> <li> An optional system-dependent <em>prefix</em> string, such as a disk-drive specifier, "/"&nbsp;for the UNIX root directory, or "\\\\"&nbsp;for a Microsoft Windows UNC pathname, and <li> A sequence of zero or more string <em>names</em>. </ol>

The first name in an abstract pathname may be a directory name or, in the case of Microsoft Windows UNC pathnames, a hostname. Each subsequent name in an abstract pathname denotes a directory; the last name may denote either a directory or a file. The <em>empty</em> abstract pathname has no prefix and an empty name sequence.

The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default <em>separator character</em>. The default name-separator character is defined by the system property file.separator, and is made available in the public static fields {@link #separator} and {@link #separatorChar} of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.

A pathname, whether abstract or in string form, may be either <em>absolute</em> or <em>relative</em>. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

The <em>parent</em> of an abstract pathname may be obtained by invoking the #getParent method of this class and consists of the pathname's prefix and each name in the pathname's name sequence except for the last. Each directory's absolute pathname is an ancestor of any File object with an absolute abstract pathname which begins with the directory's absolute pathname. For example, the directory denoted by the abstract pathname "/usr" is an ancestor of the directory denoted by the pathname "/usr/local/bin".

The prefix concept is used to handle root directories on UNIX platforms, and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms, as follows:

<ul>

<li> For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix "/" and an empty name sequence.

<li> For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by ":" and possibly followed by "\\" if the pathname is absolute. The prefix of a UNC pathname is "\\\\"; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.

</ul>

Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition "partName">named by some ancestor of the absolute form of this pathname.

A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object's owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail.

Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

<h3>Interoperability with java.nio.file package</h3>

The <c>java.nio.file</c> package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The #toPath toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. The resulting Path may be used with the java.nio.file.Files class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.

On Android strings are converted to UTF-8 byte sequences when sending filenames to the operating system, and byte sequences returned by the operating system (from the various list methods) are converted to strings by decoding them as UTF-8 byte sequences.

Added in JDK1.0.

Java documentation for java.io.File.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

File(File, String)

Creates a new File instance from a parent abstract pathname and a child pathname string.

File(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

File(String, String)

Creates a new File instance from a parent pathname string and a child pathname string.

File(String)

Creates a new File instance by converting the given pathname string into an abstract pathname.

File(URI)

Creates a new File instance by converting the given file: URI into an abstract pathname.

Properties

AbsoluteFile

Returns the absolute form of this abstract pathname.

AbsolutePath

Returns the absolute path of this file.

CanonicalFile

Returns the canonical form of this abstract pathname.

CanonicalPath

Returns the canonical pathname string of this abstract pathname.

Class

Returns the runtime class of this Object.

(Inherited from Object)
FreeSpace

Returns the number of unallocated bytes in the partition named by this abstract path name.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsAbsolute

Tests whether this abstract pathname is absolute.

IsDirectory

Tests whether the file denoted by this abstract pathname is a directory.

IsFile

Tests whether the file denoted by this abstract pathname is a normal file.

IsHidden

Tests whether the file named by this abstract pathname is a hidden file.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Name

Returns the name of the file or directory denoted by this abstract pathname.

Parent

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

ParentFile

Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Path

Converts this abstract pathname into a pathname string.

PathSeparator

The system-dependent path-separator character, represented as a string for convenience.

PathSeparatorChar

The system-dependent path-separator character.

PeerReference (Inherited from Object)
Separator

The system-dependent default name-separator character, represented as a string for convenience.

SeparatorChar

The system-dependent default name-separator character.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

TotalSpace

Returns the size of the partition named by this abstract pathname.

UsableSpace

Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.

Methods

CanExecute()

Tests whether the application can execute the file denoted by this abstract pathname.

CanRead()

Tests whether the application can read the file denoted by this abstract pathname.

CanWrite()

Tests whether the application can modify the file denoted by this abstract pathname.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CompareTo(File)

Compares two abstract pathnames lexicographically.

CreateNewFile()

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

CreateTempFile(String, String, File)

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

CreateTempFile(String, String)

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

Delete()

Deletes the file or directory denoted by this abstract pathname.

DeleteOnExit()

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Exists()

Tests whether the file or directory denoted by this abstract pathname exists.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
LastModified()

Returns the time that the file denoted by this abstract pathname was last modified.

Length()

Returns the length of the file denoted by this abstract pathname.

List()

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

List(IFilenameFilter)

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

ListAsync()
ListAsync(IFilenameFilter)
ListFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

ListFiles(IFileFilter)

Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

ListFiles(IFilenameFilter)

Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

ListFilesAsync()
ListFilesAsync(IFileFilter)
ListFilesAsync(IFilenameFilter)
ListRoots()

Returns the file system roots.

ListRootsAsync()
Mkdir()

Creates the directory named by this abstract pathname.

Mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
RenameTo(File)

Renames the file denoted by this abstract pathname.

SetExecutable(Boolean, Boolean)

Sets the owner's or everybody's execute permission for this abstract pathname.

SetExecutable(Boolean)

A convenience method to set the owner's execute permission for this abstract pathname.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetLastModified(Int64)

Sets the last-modified time of the file or directory named by this abstract pathname.

SetReadable(Boolean, Boolean)

Sets the owner's or everybody's read permission for this abstract pathname.

SetReadable(Boolean)

A convenience method to set the owner's read permission for this abstract pathname.

SetReadOnly()

Marks the file or directory named by this abstract pathname so that only read operations are allowed.

SetWritable(Boolean, Boolean)

Sets the owner's or everybody's write permission for this abstract pathname.

SetWritable(Boolean)

A convenience method to set the owner's write permission for this abstract pathname.

ToArray<T>() (Inherited from Object)
ToPath()

Returns a Path java.nio.file.Path object constructed from the this abstract path.

ToString()

Returns a string representation of the object.

(Inherited from Object)
ToURI()

Constructs a file: URI that represents this abstract pathname.

ToURL()
Obsolete.

Converts this abstract pathname into a file: URL.

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to