Android
java.io
public class

java.io.File

java.lang.Object
java.io.File Serializable Comparable<T>

File is a class which represents a file name or directory. The file may be absolute relative to the root directory of the file system or relative to the current directory in which the program is running.

This class provides methods for querying/changing information about the file and also directory listing capabilities if the File represents a directory.

When manipulating file paths, the static fields of this class may be used to determine the platform specific separators.

Summary

Constants

      Value  
String  pathSeparator  System dependent path separator String.     
char  pathSeparatorChar  System dependent path separator character.     
String  separator  System dependent file separator String.     
char  separatorChar  System dependent file separator character.     

Public Constructors

            File(File dir, String name)
Constructs a new File using the specified directory and name.
            File(String path)
Constructs a new File using the specified path.
            File(String dirPath, String name)
Constructs a new File using the specified directory and name placing a path separator between the two.
            File(URI uri)
Constructs a new File using the path of the specified URI uri needs to be an absolute and hierarchical URI with file scheme, and non-empty path component, but with undefined authority, query or fragment components.

Public Methods

          boolean  canRead()
Returns a boolean indicating whether or not the current context is allowed to read this File.
          boolean  canWrite()
Returns a boolean indicating whether or not the current context is allowed to write to this File.
          int  compareTo(File another)
Returns the relative sort ordering of paths for the receiver and given argument.
          boolean  createNewFile()
Creates the file specified by this File.
      static    File  createTempFile(String prefix, String suffix, File directory)
Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name.
      static    File  createTempFile(String prefix, String suffix)
Creates an empty temporary file using the given prefix and suffix as part of the file name.
          boolean  delete()
Deletes the file specified by this File.
          void  deleteOnExit()
When the virtual machine terminates, any abstract files which have been sent deleteOnExit() will be deleted.
          boolean  equals(Object obj)
Compares the argument obj to the receiver, and returns true if they represent the same object using a path specific comparison.
          boolean  exists()
Returns a boolean indicating whether or not this File can be found on the underlying file system.
          File  getAbsoluteFile()
Returns a new File constructed using the absolute file path of this File.
          String  getAbsolutePath()
Returns the absolute file path of this File.
          File  getCanonicalFile()
Returns a new File created using the canonical file path of this File.
          String  getCanonicalPath()
Returns the absolute file path of this File with all references resolved.
          String  getName()
Returns the filename (not directory) of this File.
          String  getParent()
Returns the pathname of the parent of this File.
          File  getParentFile()
Returns a new File made from the pathname of the parent of this File.
          String  getPath()
Returns the file path of this File.
          int  hashCode()
Returns an integer hash code for the receiver.
          boolean  isAbsolute()
Returns if this File is an absolute pathname.
          boolean  isDirectory()
Returns if this File represents a directory on the underlying file system.
          boolean  isFile()
Returns if this File represents a file on the underlying file system.
          boolean  isHidden()
Returns whether or not this file is a hidden file as defined by the operating system.
          long  lastModified()
Returns the time this File was last modified.
          long  length()
Returns the length of this File in bytes.
          String[]  list()
Returns an array of Strings representing the file names in the directory represented by this File.
          String[]  list(FilenameFilter filter)
Returns an array of Strings representing the file names in the directory represented by this File that match a specific filter.
          File[]  listFiles(FileFilter filter)
Returns an array of Files representing the file names in the directory represented by this File that match a specific filter.
          File[]  listFiles(FilenameFilter filter)
Returns an array of Files representing the file names in the directory represented by this File that match a specific filter.
          File[]  listFiles()
Returns an array of Files representing the file names in the directory represented by this File.
      static    File[]  listRoots()
Lists the filesystem roots.
          boolean  mkdir()
Creates the directory named by the trailing filename of this File.
          boolean  mkdirs()
Create all the directories needed for this File.
          boolean  renameTo(File dest)
Renames this File to the name represented by the File dest.
          boolean  setLastModified(long time)
Sets the time this File was last modified.
          boolean  setReadOnly()
Marks this file or directory to be read-only as defined by the operating system.
          String  toString()
Returns a string containing a concise, human-readable description of the receiver.
          URI  toURI()
Returns a file URI for this File.
          URL  toURL()
Returns a file URL for this File.
Methods inherited from class java.lang.Object
Methods inherited from interface java.lang.Comparable

Details

Constants

public static final String pathSeparator

System dependent path separator String. The initial value of this field is the System property "path.separator".

public static final char pathSeparatorChar

System dependent path separator character.

public static final String separator

System dependent file separator String. The initial value of this field is the System property "file.separator".

public static final char separatorChar

System dependent file separator character.

Public Constructors

public File(File dir, String name)

Constructs a new File using the specified directory and name.

Parameters

dir the directory for the file name
name the file name to be contained in the dir

public File(String path)

Constructs a new File using the specified path.

Parameters

path the path to be used for the file

public File(String dirPath, String name)

Constructs a new File using the specified directory and name placing a path separator between the two.

Parameters

dirPath the directory for the file name
name the file name to be contained in the dir

public File(URI uri)

Constructs a new File using the path of the specified URI uri needs to be an absolute and hierarchical URI with file scheme, and non-empty path component, but with undefined authority, query or fragment components.

Parameters

uri the URI instance which will be used to construct this file

Throws

IllegalArgumentException if uri does not comply with the conditions above.

See Also

Public Methods

public boolean canRead()

Returns a boolean indicating whether or not the current context is allowed to read this File.

Returns

  • true if this File can be read, false otherwise.

public boolean canWrite()

Returns a boolean indicating whether or not the current context is allowed to write to this File.

Returns

  • true if this File can be written, false otherwise.

public int compareTo(File another)

Returns the relative sort ordering of paths for the receiver and given argument. The ordering is platform dependent.

Parameters

another a File to compare the receiver to

Returns

  • an int determined by comparing the two paths. The meaning is described in the Comparable interface.

See Also

public boolean createNewFile()

Creates the file specified by this File. If the file already exists this method returns false. Otherwise, if the file is created successfully, the result is true. An IOException will be thrown if the directory to contain this file does not exist.

Returns

  • true if this File was created, false otherwise.

Throws

IOException if an I/O error occurs or the directory does not exist.

public static File createTempFile(String prefix, String suffix, File directory)

Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name.

Parameters

prefix the prefix to the temp file name
suffix the suffix to the temp file name
directory the location to which the temp file is to be written, or null for the default temp location

Returns

  • the temporary file

Throws

IOException If an error occurs when writing the file

public static File createTempFile(String prefix, String suffix)

Creates an empty temporary file using the given prefix and suffix as part of the file name. If suffix is null, .tmp is used.

Parameters

prefix the prefix to the temp file name
suffix the suffix to the temp file name

Returns

  • the temporary file

Throws

IOException If an error occurs when writing the file

public boolean delete()

Deletes the file specified by this File. Directories must be empty before they will be deleted.

Returns

  • true if this File was deleted, false otherwise.

Throws

SecurityException in case a SecurityManager is installed, and it denies the request.

public void deleteOnExit()

When the virtual machine terminates, any abstract files which have been sent deleteOnExit() will be deleted. This will only happen when the virtual machine terminates normally as described by the Java Language Specification section 12.9.

Throws

SecurityException in case a SecurityManager is installed, and it denies the request.

public boolean equals(Object obj)

Compares the argument obj to the receiver, and returns true if they represent the same object using a path specific comparison.

Parameters

obj the Object to compare with this Object

Returns

  • true if the object is the same as this object, false otherwise.

public boolean exists()

Returns a boolean indicating whether or not this File can be found on the underlying file system.

Returns

  • true if this File exists, false otherwise.

public File getAbsoluteFile()

Returns a new File constructed using the absolute file path of this File.

Returns

  • a new File from this absolute file path

public String getAbsolutePath()

Returns the absolute file path of this File.

Returns

  • the absolute file path

public File getCanonicalFile()

Returns a new File created using the canonical file path of this File. Equivalent to new File(this.getCanonicalPath()).

Returns

  • the canonical file path

Throws

IOException If an IO error occurs

public String getCanonicalPath()

Returns the absolute file path of this File with all references resolved. An absolute file path is one which begins at the root of the file system. The canonical path is one in which all references have been resolved. For the cases of '..' and '.' where the file system supports parent and working directory respectively, these should be removed and replaced with a direct directory reference. If the File does not exist, getCanonicalPath() may not resolve any references and simply return an absolute path name or throw an IOException.

Returns

  • the canonical file path

Throws

IOException if an IO error occurs

public String getName()

Returns the filename (not directory) of this File.

Returns

  • the filename or empty string

public String getParent()

Returns the pathname of the parent of this File. This is the path up to but not including the last name. null is returned when there is no parent.

Returns

  • the parent name or null

public File getParentFile()

Returns a new File made from the pathname of the parent of this File. This is the path up to but not including the last name. null is returned when there is no parent.

Returns

  • a new File representing parent or null

public String getPath()

Returns the file path of this File.

Returns

  • the file path

public int hashCode()

Returns an integer hash code for the receiver. Any two objects which answer true when passed to equals must answer the same value for this method.

Returns

  • the receiver's hash

See Also

public boolean isAbsolute()

Returns if this File is an absolute pathname. Whether a pathname is absolute is platform specific. On UNIX it is if the path starts with the character '/', on Windows it is absolute if either it starts with '\', '/', '\\' (to represent a file server), or a letter followed by a colon.

Returns

  • true if this File is absolute, false otherwise.

See Also

public boolean isDirectory()

Returns if this File represents a directory on the underlying file system.

Returns

  • true if this File is a directory, false otherwise.

public boolean isFile()

Returns if this File represents a file on the underlying file system.

Returns

  • true if this File is a file, false otherwise.

public boolean isHidden()

Returns whether or not this file is a hidden file as defined by the operating system.

Returns

  • true if the file is hidden, false otherwise.

public long lastModified()

Returns the time this File was last modified.

Returns

  • the time this File was last modified.

public long length()

Returns the length of this File in bytes.

Returns

  • the number of bytes in the file.

public String[] list()

Returns an array of Strings representing the file names in the directory represented by this File. If this File is not a directory the result is null.

The entries . and .. representing current directory and parent directory are not returned as part of the list.

Returns

  • an array of Strings or null.

public String[] list(FilenameFilter filter)

Returns an array of Strings representing the file names in the directory represented by this File that match a specific filter. If this File is not a directory the result is null. If the filter is null then all filenames match.

The entries . and .. representing current directory and parent directory are not returned as part of the list.

Parameters

filter the filter to match names to or null.

Returns

  • an array of Strings or null.

public File[] listFiles(FileFilter filter)

Returns an array of Files representing the file names in the directory represented by this File that match a specific filter. If this File is not a directory the result is null. If the filter is null then all filenames match.

The entries . and .. representing current directory and parent directory are not returned as part of the list.

Parameters

filter the filter to match names to or null.

Returns

  • an array of Files or null.

public File[] listFiles(FilenameFilter filter)

Returns an array of Files representing the file names in the directory represented by this File that match a specific filter. If this File is not a directory the result is null. If the filter is null then all filenames match.

The entries . and .. representing current directory and parent directory are not returned as part of the list.

Parameters

filter the filter to match names to or null.

Returns

  • an array of Files or null.

public File[] listFiles()

Returns an array of Files representing the file names in the directory represented by this File. If this File is not a directory the result is null. The Files returned will be absolute if this File is absolute, relative otherwise.

Returns

  • an array of Files or null.

public static File[] listRoots()

Lists the filesystem roots. The Java platform may support zero or more filesystems, each with its own platform-dependent root. Further, the canonical pathname of any file on the system will always begin with one of the returned filesystem roots.

Returns

  • the array of filesystem roots

public boolean mkdir()

Creates the directory named by the trailing filename of this File. Not all directories required to create this File are created.

Returns

  • true if the directory was created, false otherwise.

public boolean mkdirs()

Create all the directories needed for this File. If the terminal directory already exists, answer false. If the directories were created successfully, answer true.

Returns

  • true if the necessary directories were created, false otherwise.

public boolean renameTo(File dest)

Renames this File to the name represented by the File dest. This works for both normal files and directories.

Parameters

dest the File containing the new name.

Returns

  • true if the File was renamed, false otherwise.

public boolean setLastModified(long time)

Sets the time this File was last modified.

Parameters

time The time to set the file as last modified.

Returns

  • the time this File was last modified.

public boolean setReadOnly()

Marks this file or directory to be read-only as defined by the operating system.

Returns

  • true if the operation was a success, false otherwise

public String toString()

Returns a string containing a concise, human-readable description of the receiver.

Returns

  • a printable representation for the receiver.

public URI toURI()

Returns a file URI for this File. The URI is System dependent and may not be transferable between different operating/file systems.

Returns

  • a file URI for this File.

public URL toURL()

Returns a file URL for this File. The URL is System dependent and may not be transferable between different operating/file systems.

Returns

  • a file URL for this File.

Throws

MalformedURLException if the path cannot be transformed into an URL
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56