Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Class File: Java - Io

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

java.

io
Class File
java.lang.Object
|
+--java.io.File

public class File


extends Object
implements Serializable, Comparable

An abstract representation of file and directory pathnames.


User interfaces and operating systems use system-dependent pathname strings to name files and
directories. This class presents an abstract, system-independent view of hierarchical pathnames.

Field Summary
· public static final char separatorChar
The system-dependent default name-separator character. This field is initialized to contain the first
character of the value of the system property file.separator. On UNIX systems the value of this
field is '/'; on Win32 systems it is '\'.
· public static final String separator
The system-dependent default name-separator character, represented as a string for convenience.
This string contains a single character, namely separatorChar.
· public static final char pathSeparatorChar
The system-dependent path-separator character. This field is initialized to contain the first character
of the value of the system property path.separator. This character is used to separate filenames
in a sequence of files given as a path list. On UNIX systems, this character is ':'; on Win32
systems it is ';'.
· public static final String pathSeparator
The system-dependent path-separator character, represented as a string for convenience. This string
contains a single character, namely pathSeparatorChar.

Constructor Summary
· public File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. If
the given string is the empty string, then the result is the empty abstract pathname.
Throws:
NullPointerException - If the pathname argument is null
· public File(String parent,String child)
Throws:
NullPointerException - If child is null
· public File(File parent,String child)
Throws:
NullPointerException - If child is null
Method Summary
· public String getName()
Returns the name of the file or directory denoted by this abstract pathname. This is just the last
name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty
string is returned.
· public String getParent()
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not
name a parent directory.
The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the
pathname's name sequence except for the last. If the name sequence is empty then the pathname
does not name a parent directory.
· public String getPath()
Converts this abstract pathname into a pathname string. The resulting string uses the default name-
separator character to separate the names in the name sequence.
· public boolean isAbsolute()
Tests whether this abstract pathname is absolute. The definition of absolute pathname is system
dependent. On UNIX systems, a pathname is absolute if its prefix is "/". On Win32 systems, a
pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\".
· public String getAbsolutePath()
Returns the absolute pathname string of this abstract pathname.
public File getAbsoluteFile()
Returns the absolute form of this abstract pathname. Equivalent to new
File(this.getAbsolutePath()()).
· public URL toURL()throws MalformedURLException
Converts this abstract pathname into a file: URL. The exact form of the URL is system-
dependent. If it can be determined that the file denoted by this abstract pathname is a directory,
then the resulting URL will end with a slash.
· public boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname. Returns true if
and only if the file specified by this abstract pathname exists and can be read by the application;
false otherwise
Throws:
SecurityException - If a security manager exists and it denies read access to the file
· public boolean canWrite()
Tests whether the application can modify to the file denoted by this abstract pathname.
Returns true if and only if the file system actually contains a file denoted by this abstract
pathname and the application is allowed to write to the file; false otherwise.
Throws:
SecurityException - If a security manager exists and denies write access to the file
· public boolean exists()
Tests whether the file denoted by this abstract pathname exists.
Returns true if and only if the file denoted by this abstract pathname exists; false otherwise
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
Returns true if and only if the file denoted by this abstract pathname exists and is a directory;
false otherwise
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not
a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file
created by a Java application is guaranteed to be a normal file.
Returns true if and only if the file denoted by this abstract pathname exists and is a normal file;
false otherwise
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public boolean isHidden()
Tests whether the file named by this abstract pathname is a hidden file. The exact definition of
hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name begins
with a period character ('.'). On Win32 systems, a file is considered to be hidden if it has been
marked as such in the filesystem.
Returns true if and only if the file denoted by this abstract pathname is hidden according to the
conventions of the underlying platform
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public long lastModified()
Returns the time that the file denoted by this abstract pathname was last modified.
A long value representing the time the file was last modified, measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970.
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public long length()
Returns the length of the file denoted by this abstract pathname.
Throws:
SecurityException - If a security manager exists and its denies read access to the file
· public boolean createNewFile()throws IOException
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this
name does not yet exist.
Returns true if the named file does not exist and was successfully created; false if the named
file already exists
Throws:
IOException - If an I/O error occurred
SecurityException - If a security manager exists and its denies write access to the file
· public boolean delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a
directory, then the directory must be empty in order to be deleted.
Returns true if and only if the file or directory is successfully deleted; false otherwise
Throws:
SecurityException - If a security manager exists and its denies delete access to the file
· public void deleteOnExit()
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual
machine terminates.
Once deletion has been requested, it is not possible to cancel the request. This method should
therefore be used with care.
Throws:
SecurityException - If a security manager exists and its denies delete access to the file
· public String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract
pathname.
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an
array of strings is returned, one for each file or directory in the directory. Names denoting the
directory itself and the directory's parent directory are not included in the result. Each string is a
file name rather than a complete path.
There is no guarantee that the name strings in the resulting array will appear in any specific order;
they are not, in particular, guaranteed to appear in alphabetical order.
Throws:
SecurityException - If a security manager exists and its denies read access to the directory
· public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract
pathname.
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an
array of File objects is returned, one for each file or directory in the directory. Pathnames
denoting the directory itself and the directory's parent directory are not included in the result. Each
resulting abstract pathname is constructed from this abstract pathname using the File(File,
String) constructor. Therefore if this pathname is absolute then each resulting pathname is
absolute; if this pathname is relative then each resulting pathname will be relative to the same
directory.
There is no guarantee that the name strings in the resulting array will appear in any specific order;
they are not, in particular, guaranteed to appear in alphabetical order.
Throws:
SecurityException - If a security manager exists and its denies read access to the directory
· public boolean mkdir()
Creates the directory named by this abstract pathname.
Returns true if and only if the directory was created; false otherwise
Throws:
SecurityException - If a security manager exists and its does not permit the named directory to be
created
· public boolean mkdirs()
Creates the directory named by this abstract pathname, including any necessary but nonexistent
parent directories. Note that if this operation fails it may have succeeded in creating some of the
necessary parent directories.
Returns true if and only if the directory was created, along with all necessary parent directories;
false otherwise
Throws:
SecurityException - If a security manager exists and its does not permit the named directory and all
necessary parent directories and to be created
· public boolean renameTo(File dest)
Renames the file denoted by this abstract pathname.
Returns true if and only if the renaming succeeded; false otherwise
Throws:
SecurityException - If a security manager exists and its denies write access to both the old and new
pathnames
· public boolean setLastModified(long time)
Sets the last-modified time of the file or directory named by this abstract pathname.
Throws:
IllegalArgumentException - If the argument is negative
SecurityException - If a security manager exists and its denies write access to the named file
· public boolean setReadOnly()
Marks the file or directory named by this abstract pathname so that only read operations are
allowed. After invoking this method the file or directory is guaranteed not to change until it is
either deleted or marked to allow write access. Whether or not a read-only file or directory may be
deleted depends upon the underlying system.
Throws:
SecurityException - If a security manager exists and its denies write access to the named file
· public static File[] listRoots()
List the available filesystem roots.
A particular Java platform may support zero or more hierarchically-organized file systems. Each
file system has a root directory from which all other files in that file system can be reached.
Windows platforms, for example, have a root directory for each active drive; UNIX platforms have
a single root directory, namely "/".
This method returns an array of File objects that denote the root directories of the available
filesystem roots.
· public int compareTo(File pathname)
· public int compareTo(Object o)
· public boolean equals(Object obj)
· public String toString()

You might also like