jdk/src/share/classes/java/io/File.java
author malenkov
Wed, 18 Jun 2008 19:15:23 +0400
changeset 681 b375ecec621f
parent 2 90ce3da70b43
child 2601 39743edb9b8b
child 715 f16baef3a20e
permissions -rw-r--r--
6708550: LTP: XMLEncoder does not encode instances of the File class Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
681
b375ecec621f 6708550: LTP: XMLEncoder does not encode instances of the File class
malenkov
parents: 2
diff changeset
    28
import java.beans.ConstructorProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessControlException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An abstract representation of file and directory pathnames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> User interfaces and operating systems use system-dependent <em>pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * strings</em> to name files and directories.  This class presents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * abstract, system-independent view of hierarchical pathnames.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <em>abstract pathname</em> has two components:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li> An optional system-dependent <em>prefix</em> string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      such as a disk-drive specifier, <code>"/"</code>&nbsp;for the UNIX root
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      directory, or <code>"\\\\"</code>&nbsp;for a Microsoft Windows UNC pathname, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li> A sequence of zero or more string <em>names</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The first name in an abstract pathname may be a directory name or, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * case of Microsoft Windows UNC pathnames, a hostname.  Each subsequent name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * in an abstract pathname denotes a directory; the last name may denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * either a directory or a file.  The <em>empty</em> abstract pathname has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * prefix and an empty name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> The conversion of a pathname string to or from an abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * inherently system-dependent.  When an abstract pathname is converted into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * pathname string, each name is separated from the next by a single copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the default <em>separator character</em>.  The default name-separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * character is defined by the system property <code>file.separator</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * is made available in the public static fields <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * #separator}</code> and <code>{@link #separatorChar}</code> of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * When a pathname string is converted into an abstract pathname, the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * within it may be separated by the default name-separator character or by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * other name-separator character that is supported by the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p> A pathname, whether abstract or in string form, may be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <em>absolute</em> or <em>relative</em>.  An absolute pathname is complete in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * that no other information is required in order to locate the file that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * denotes.  A relative pathname, in contrast, must be interpreted in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * information taken from some other pathname.  By default the classes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>java.io</code> package always resolve relative pathnames against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * current user directory.  This directory is named by the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>user.dir</code>, and is typically the directory in which the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * virtual machine was invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p> The <em>parent</em> of an abstract pathname may be obtained by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * the {@link #getParent} method of this class and consists of the pathname's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * prefix and each name in the pathname's name sequence except for the last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Each directory's absolute pathname is an ancestor of any <tt>File</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * object with an absolute abstract pathname which begins with the directory's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * absolute pathname.  For example, the directory denoted by the abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * pathname <tt>"/usr"</tt> is an ancestor of the directory denoted by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * pathname <tt>"/usr/local/bin"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> The prefix concept is used to handle root directories on UNIX platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <li> For UNIX platforms, the prefix of an absolute pathname is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <code>"/"</code>.  Relative pathnames have no prefix.  The abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * denoting the root directory has the prefix <code>"/"</code> and an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <li> For Microsoft Windows platforms, the prefix of a pathname that contains a drive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * specifier consists of the drive letter followed by <code>":"</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * possibly followed by <code>"\\"</code> if the pathname is absolute.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * prefix of a UNC pathname is <code>"\\\\"</code>; the hostname and the share
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * name are the first two names in the name sequence.  A relative pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * does not specify a drive has no prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p> Instances of this class may or may not denote an actual file-system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * object such as a file or a directory.  If it does denote such an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * then that object resides in a <i>partition</i>.  A partition is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * operating system-specific portion of storage for a file system.  A single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * contain multiple partitions.  The object, if any, will reside on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * partition <a name="partName">named</a> by some ancestor of the absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * form of this pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p> A file system may implement restrictions to certain operations on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * actual file-system object, such as reading, writing, and executing.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * restrictions are collectively known as <i>access permissions</i>.  The file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * system may have multiple sets of access permissions on a single object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * For example, one set may apply to the object's <i>owner</i>, and another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * may apply to all other users.  The access permissions on an object may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * cause some methods in this class to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p> Instances of the <code>File</code> class are immutable; that is, once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * created, the abstract pathname represented by a <code>File</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * will never change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
public class File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    implements Serializable, Comparable<File>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The FileSystem object representing the platform's local file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static private FileSystem fs = FileSystem.getFileSystem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * This abstract pathname's normalized pathname string.  A normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * pathname string uses the default name-separator character and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * contain any duplicate or redundant separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * The length of this abstract pathname's prefix, or zero if it has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private transient int prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Returns the length of this abstract pathname's prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * For use by FileSystem classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    int getPrefixLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The system-dependent default name-separator character.  This field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * initialized to contain the first character of the value of the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * property <code>file.separator</code>.  On UNIX systems the value of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * field is <code>'/'</code>; on Microsoft Windows systems it is <code>'\\'</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static final char separatorChar = fs.getSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The system-dependent default name-separator character, represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * string for convenience.  This string contains a single character, namely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code>{@link #separatorChar}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public static final String separator = "" + separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * The system-dependent path-separator character.  This field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * initialized to contain the first character of the value of the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * property <code>path.separator</code>.  This character is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * separate filenames in a sequence of files given as a <em>path list</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * On UNIX systems, this character is <code>':'</code>; on Microsoft Windows systems it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * is <code>';'</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static final char pathSeparatorChar = fs.getPathSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The system-dependent path-separator character, represented as a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * for convenience.  This string contains a single character, namely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>{@link #pathSeparatorChar}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final String pathSeparator = "" + pathSeparatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /* -- Constructors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Internal constructor for already-normalized pathname strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private File(String pathname, int prefixLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.path = pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.prefixLength = prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Internal constructor for already-normalized pathname strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * The parameter order is used to disambiguate this method from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * public(File, String) constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private File(String child, File parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        assert parent.path != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        assert (!parent.path.equals(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this.path = fs.resolve(parent.path, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        this.prefixLength = parent.prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Creates a new <code>File</code> instance by converting the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * pathname string into an abstract pathname.  If the given string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * the empty string, then the result is the empty abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param   pathname  A pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          If the <code>pathname</code> argument is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
681
b375ecec621f 6708550: LTP: XMLEncoder does not encode instances of the File class
malenkov
parents: 2
diff changeset
   238
    @ConstructorProperties("path")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public File(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (pathname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        this.path = fs.normalize(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /* Note: The two-argument File constructors do not interpret an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
       parent abstract pathname as the current user directory.  An empty parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
       instead causes the child to be resolved against the system-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
       directory defined by the FileSystem.getDefaultParent method.  On Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
       this default is "/", while on Microsoft Windows it is "\\".  This is required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
       compatibility with the original behavior of this class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates a new <code>File</code> instance from a parent pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * and a child pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p> If <code>parent</code> is <code>null</code> then the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <code>File</code> instance is created as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * single-argument <code>File</code> constructor on the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>child</code> pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p> Otherwise the <code>parent</code> pathname string is taken to denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * a directory, and the <code>child</code> pathname string is taken to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * denote either a directory or a file.  If the <code>child</code> pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * string is absolute then it is converted into a relative pathname in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * system-dependent way.  If <code>parent</code> is the empty string then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * the new <code>File</code> instance is created by converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>child</code> into an abstract pathname and resolving the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * against a system-dependent default directory.  Otherwise each pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * string is converted into an abstract pathname and the child abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * pathname is resolved against the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param   parent  The parent pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param   child   The child pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          If <code>child</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public File(String parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (parent.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                this.path = fs.resolve(fs.getDefaultParent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                this.path = fs.resolve(fs.normalize(parent),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            this.path = fs.normalize(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Creates a new <code>File</code> instance from a parent abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * pathname and a child pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p> If <code>parent</code> is <code>null</code> then the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code>File</code> instance is created as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * single-argument <code>File</code> constructor on the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <code>child</code> pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p> Otherwise the <code>parent</code> abstract pathname is taken to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * denote a directory, and the <code>child</code> pathname string is taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * to denote either a directory or a file.  If the <code>child</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * pathname string is absolute then it is converted into a relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * pathname in a system-dependent way.  If <code>parent</code> is the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * abstract pathname then the new <code>File</code> instance is created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * converting <code>child</code> into an abstract pathname and resolving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * the result against a system-dependent default directory.  Otherwise each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * pathname string is converted into an abstract pathname and the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * abstract pathname is resolved against the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param   parent  The parent abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param   child   The child pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *          If <code>child</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public File(File parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (parent.path.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                this.path = fs.resolve(fs.getDefaultParent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                this.path = fs.resolve(parent.path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            this.path = fs.normalize(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Creates a new <tt>File</tt> instance by converting the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <tt>file:</tt> URI into an abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * the transformation performed by this constructor is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * system-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p> For a given abstract pathname <i>f</i> it is guaranteed that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <blockquote><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * </tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * so long as the original abstract pathname, the URI, and the new abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * pathname are all created in (possibly different invocations of) the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Java virtual machine.  This relationship typically does not hold,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * however, when a <tt>file:</tt> URI that is created in a virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * on one operating system is converted into an abstract pathname in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * virtual machine on a different operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param  uri
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         An absolute, hierarchical URI with a scheme equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *         <tt>"file"</tt>, a non-empty path component, and undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *         authority, query, and fragment components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *          If <tt>uri</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *          If the preconditions on the parameter do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see #toURI()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public File(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Check our many preconditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (!uri.isAbsolute())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("URI is not absolute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (uri.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new IllegalArgumentException("URI is not hierarchical");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        String scheme = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new IllegalArgumentException("URI scheme is not \"file\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (uri.getAuthority() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throw new IllegalArgumentException("URI has an authority component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (uri.getFragment() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new IllegalArgumentException("URI has a fragment component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (uri.getQuery() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new IllegalArgumentException("URI has a query component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        String p = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (p.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new IllegalArgumentException("URI path component is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // Okay, now initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        p = fs.fromURIPath(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (File.separatorChar != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            p = p.replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        this.path = fs.normalize(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /* -- Path-component accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Returns the name of the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * pathname.  This is just the last name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * sequence.  If the pathname's name sequence is empty, then the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * string is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return  The name of the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          pathname, or the empty string if this pathname's name sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *          is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int index = path.lastIndexOf(separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (index < prefixLength) return path.substring(prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return path.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Returns the pathname string of this abstract pathname's parent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <code>null</code> if this pathname does not name a parent directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p> The <em>parent</em> of an abstract pathname consists of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * pathname's prefix, if any, and each name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * sequence except for the last.  If the name sequence is empty then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * the pathname does not name a parent directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return  The pathname string of the parent directory named by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *          abstract pathname, or <code>null</code> if this pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *          does not name a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public String getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        int index = path.lastIndexOf(separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (index < prefixLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if ((prefixLength > 0) && (path.length() > prefixLength))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                return path.substring(0, prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return path.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns the abstract pathname of this abstract pathname's parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * or <code>null</code> if this pathname does not name a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p> The <em>parent</em> of an abstract pathname consists of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * pathname's prefix, if any, and each name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * sequence except for the last.  If the name sequence is empty then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * the pathname does not name a parent directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return  The abstract pathname of the parent directory named by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *          abstract pathname, or <code>null</code> if this pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *          does not name a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public File getParentFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        String p = this.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (p == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return new File(p, this.prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Converts this abstract pathname into a pathname string.  The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * string uses the {@link #separator default name-separator character} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * separate the names in the name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return  The string form of this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Tests whether this abstract pathname is absolute.  The definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * absolute pathname is system dependent.  On UNIX systems, a pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * absolute if its prefix is <code>"/"</code>.  On Microsoft Windows systems, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * pathname is absolute if its prefix is a drive specifier followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <code>"\\"</code>, or if its prefix is <code>"\\\\"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @return  <code>true</code> if this abstract pathname is absolute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public boolean isAbsolute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return fs.isAbsolute(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Returns the absolute pathname string of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <p> If this abstract pathname is already absolute, then the pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * string is simply returned as if by the <code>{@link #getPath}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * method.  If this abstract pathname is the empty abstract pathname then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * the pathname string of the current user directory, which is named by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * system property <code>user.dir</code>, is returned.  Otherwise this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * pathname is resolved in a system-dependent way.  On UNIX systems, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * relative pathname is made absolute by resolving it against the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * user directory.  On Microsoft Windows systems, a relative pathname is made absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * by resolving it against the current directory of the drive named by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * pathname, if any; if not, it is resolved against the current user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return  The absolute pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *          If a required system property value cannot be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @see     java.io.File#isAbsolute()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public String getAbsolutePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return fs.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Returns the absolute form of this abstract pathname.  Equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * <code>new&nbsp;File(this.{@link #getAbsolutePath})</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @return  The absolute abstract pathname denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *          If a required system property value cannot be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public File getAbsoluteFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String absPath = getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return new File(absPath, fs.prefixLength(absPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns the canonical pathname string of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <p> A canonical pathname is both absolute and unique.  The precise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * definition of canonical form is system-dependent.  This method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * converts this pathname to absolute form if necessary, as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * {@link #getAbsolutePath} method, and then maps it to its unique form in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * system-dependent way.  This typically involves removing redundant names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * such as <tt>"."</tt> and <tt>".."</tt> from the pathname, resolving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * symbolic links (on UNIX platforms), and converting drive letters to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * standard case (on Microsoft Windows platforms).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p> Every pathname that denotes an existing file or directory has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * unique canonical form.  Every pathname that denotes a nonexistent file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * or directory also has a unique canonical form.  The canonical form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * the pathname of a nonexistent file or directory may be different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * the canonical form of the same pathname after the file or directory is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * created.  Similarly, the canonical form of the pathname of an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * file or directory may be different from the canonical form of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * pathname after the file or directory is deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @return  The canonical pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *          If an I/O error occurs, which is possible because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *          construction of the canonical pathname may require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *          filesystem queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          If a required system property value cannot be accessed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *          java.lang.SecurityManager#checkRead}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *          read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public String getCanonicalPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return fs.canonicalize(fs.resolve(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Returns the canonical form of this abstract pathname.  Equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>new&nbsp;File(this.{@link #getCanonicalPath})</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return  The canonical pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *          If an I/O error occurs, which is possible because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *          construction of the canonical pathname may require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *          filesystem queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *          If a required system property value cannot be accessed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *          java.lang.SecurityManager#checkRead}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *          read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public File getCanonicalFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        String canonPath = getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return new File(canonPath, fs.prefixLength(canonPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private static String slashify(String path, boolean isDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        String p = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (File.separatorChar != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            p = p.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (!p.startsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            p = "/" + p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (!p.endsWith("/") && isDirectory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            p = p + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Converts this abstract pathname into a <code>file:</code> URL.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * exact form of the URL is system-dependent.  If it can be determined that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * the file denoted by this abstract pathname is a directory, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * resulting URL will end with a slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @return  A URL object representing the equivalent file URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @throws  MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *          If the path cannot be parsed as a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see     #toURI()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @see     java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @see     java.net.URI#toURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @see     java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @deprecated This method does not automatically escape characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * are illegal in URLs.  It is recommended that new code convert an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * abstract pathname into a URL by first converting it into a URI, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * {@link #toURI() toURI} method, and then converting the URI into a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * via the {@link java.net.URI#toURL() URI.toURL} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public URL toURL() throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Constructs a <tt>file:</tt> URI that represents this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <p> The exact form of the URI is system-dependent.  If it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * determined that the file denoted by this abstract pathname is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * directory, then the resulting URI will end with a slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <p> For a given abstract pathname <i>f</i>, it is guaranteed that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <blockquote><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * new {@link #File(java.net.URI) File}(</tt><i>&nbsp;f</i><tt>.toURI()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * </tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * so long as the original abstract pathname, the URI, and the new abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * pathname are all created in (possibly different invocations of) the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Java virtual machine.  Due to the system-dependent nature of abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * pathnames, however, this relationship typically does not hold when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <tt>file:</tt> URI that is created in a virtual machine on one operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * system is converted into an abstract pathname in a virtual machine on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * different operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @return  An absolute, hierarchical URI with a scheme equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *          <tt>"file"</tt>, a path representing this abstract pathname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *          and undefined authority, query, and fragment components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @throws SecurityException If a required system property value cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see #File(java.net.URI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @see java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see java.net.URI#toURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public URI toURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            File f = getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            String sp = slashify(f.getPath(), f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (sp.startsWith("//"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                sp = "//" + sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return new URI("file", null, sp, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throw new Error(x);         // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Tests whether the application can read the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @return  <code>true</code> if and only if the file specified by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *          abstract pathname exists <em>and</em> can be read by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *          application; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public boolean canRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return fs.checkAccess(this, FileSystem.ACCESS_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Tests whether the application can modify the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @return  <code>true</code> if and only if the file system actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *          contains a file denoted by this abstract pathname <em>and</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *          the application is allowed to write to the file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public boolean canWrite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Tests whether the file or directory denoted by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @return  <code>true</code> if and only if the file or directory denoted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *          by this abstract pathname exists; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *          method denies read access to the file or directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    public boolean exists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Tests whether the file denoted by this abstract pathname is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @return <code>true</code> if and only if the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *          abstract pathname exists <em>and</em> is a directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Tests whether the file denoted by this abstract pathname is a normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * file.  A file is <em>normal</em> if it is not a directory and, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * addition, satisfies other system-dependent criteria.  Any non-directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * file created by a Java application is guaranteed to be a normal file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @return  <code>true</code> if and only if the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *          abstract pathname exists <em>and</em> is a normal file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public boolean isFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Tests whether the file named by this abstract pathname is a hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * file.  The exact definition of <em>hidden</em> is system-dependent.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * UNIX systems, a file is considered to be hidden if its name begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * a period character (<code>'.'</code>).  On Microsoft Windows systems, a file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * considered to be hidden if it has been marked as such in the filesystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @return  <code>true</code> if and only if the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *          abstract pathname is hidden according to the conventions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *          underlying platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public boolean isHidden() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * Returns the time that the file denoted by this abstract pathname was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * last modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return  A <code>long</code> value representing the time the file was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *          last modified, measured in milliseconds since the epoch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *          (00:00:00 GMT, January 1, 1970), or <code>0L</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *          file does not exist or if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public long lastModified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return fs.getLastModifiedTime(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Returns the length of the file denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * The return value is unspecified if this pathname denotes a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @return  The length, in bytes, of the file denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *          pathname, or <code>0L</code> if the file does not exist.  Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *          operating systems may return <code>0L</code> for pathnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *          denoting system-dependent entities such as devices or pipes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        return fs.getLength(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Atomically creates a new, empty file named by this abstract pathname if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * and only if a file with this name does not yet exist.  The check for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * existence of the file and the creation of the file if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * are a single operation that is atomic with respect to all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * filesystem activities that might affect the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Note: this method should <i>not</i> be used for file-locking, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * the resulting protocol cannot be made to work reliably. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * {@link java.nio.channels.FileLock FileLock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * facility should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @return  <code>true</code> if the named file does not exist and was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *          successfully created; <code>false</code> if the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *          already exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *          If an I/O error occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public boolean createNewFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        if (security != null) security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return fs.createFileExclusively(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Deletes the file or directory denoted by this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * this pathname denotes a directory, then the directory must be empty in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * order to be deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @return  <code>true</code> if and only if the file or directory is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *          successfully deleted; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *          java.lang.SecurityManager#checkDelete}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *          delete access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    public boolean delete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            security.checkDelete(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return fs.delete(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Requests that the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * pathname be deleted when the virtual machine terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Files (or directories) are deleted in the reverse order that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * they are registered. Invoking this method to delete a file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * directory that is already registered for deletion has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Deletion will be attempted only for normal termination of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * virtual machine, as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * <p> Once deletion has been requested, it is not possible to cancel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * request.  This method should therefore be used with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Note: this method should <i>not</i> be used for file-locking, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * the resulting protocol cannot be made to work reliably. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * {@link java.nio.channels.FileLock FileLock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * facility should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *          java.lang.SecurityManager#checkDelete}</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *          delete access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @see #delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    public void deleteOnExit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            security.checkDelete(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        DeleteOnExitHook.add(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Returns an array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <p> If this abstract pathname does not denote a directory, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * method returns {@code null}.  Otherwise an array of strings is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * returned, one for each file or directory in the directory.  Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * denoting the directory itself and the directory's parent directory are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * not included in the result.  Each string is a file name rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * complete path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <p> There is no guarantee that the name strings in the resulting array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * will appear in any specific order; they are not, in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * guaranteed to appear in alphabetical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @return  An array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *          directory denoted by this abstract pathname.  The array will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *          empty if the directory is empty.  Returns {@code null} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *          this abstract pathname does not denote a directory, or if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *          I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public String[] list() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        return fs.list(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Returns an array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * directory denoted by this abstract pathname that satisfy the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * filter.  The behavior of this method is the same as that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * {@link #list()} method, except that the strings in the returned array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * must satisfy the filter.  If the given {@code filter} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * then all names are accepted.  Otherwise, a name satisfies the filter if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * and only if the value {@code true} results when the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * FilenameFilter#accept FilenameFilter.accept(File,&nbsp;String)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * of the filter is invoked on this abstract pathname and the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * file or directory in the directory that it denotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *         A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @return  An array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *          directory denoted by this abstract pathname that were accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *          by the given {@code filter}.  The array will be empty if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *          directory is empty or if no names were accepted by the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *          Returns {@code null} if this abstract pathname does not denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *          a directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    public String[] list(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        String names[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        if ((names == null) || (filter == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        ArrayList v = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        for (int i = 0 ; i < names.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (filter.accept(this, names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                v.add(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        return (String[])(v.toArray(new String[v.size()]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Returns an array of abstract pathnames denoting the files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <p> If this abstract pathname does not denote a directory, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * method returns {@code null}.  Otherwise an array of {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * is returned, one for each file or directory in the directory.  Pathnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * denoting the directory itself and the directory's parent directory are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * not included in the result.  Each resulting abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * constructed from this abstract pathname using the {@link #File(File,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * String) File(File,&nbsp;String)} constructor.  Therefore if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * pathname is absolute then each resulting pathname is absolute; if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * pathname is relative then each resulting pathname will be relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * the same directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * <p> There is no guarantee that the name strings in the resulting array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * will appear in any specific order; they are not, in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * guaranteed to appear in alphabetical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public File[] listFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        String[] ss = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if (ss == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        int n = ss.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        File[] fs = new File[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            fs[i] = new File(ss[i], this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return fs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Returns an array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * directories in the directory denoted by this abstract pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * satisfy the specified filter.  The behavior of this method is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * as that of the {@link #listFiles()} method, except that the pathnames in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * the returned array must satisfy the filter.  If the given {@code filter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * satisfies the filter if and only if the value {@code true} results when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * the {@link FilenameFilter#accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * FilenameFilter.accept(File,&nbsp;String)} method of the filter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * invoked on this abstract pathname and the name of a file or directory in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * the directory that it denotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *         A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    public File[] listFiles(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        String ss[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (ss == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        ArrayList<File> files = new ArrayList<File>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        for (String s : ss)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            if ((filter == null) || filter.accept(this, s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                files.add(new File(s, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        return files.toArray(new File[files.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * Returns an array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * directories in the directory denoted by this abstract pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * satisfy the specified filter.  The behavior of this method is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * as that of the {@link #listFiles()} method, except that the pathnames in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * the returned array must satisfy the filter.  If the given {@code filter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * satisfies the filter if and only if the value {@code true} results when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * the {@link FileFilter#accept FileFilter.accept(File)} method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * filter is invoked on the pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *         A file filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public File[] listFiles(FileFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        String ss[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (ss == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        ArrayList<File> files = new ArrayList<File>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        for (String s : ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            File f = new File(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if ((filter == null) || filter.accept(f))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                files.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return files.toArray(new File[files.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Creates the directory named by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @return  <code>true</code> if and only if the directory was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *          created; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *          method does not permit the named directory to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    public boolean mkdir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        return fs.createDirectory(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Creates the directory named by this abstract pathname, including any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * necessary but nonexistent parent directories.  Note that if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * operation fails it may have succeeded in creating some of the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * parent directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @return  <code>true</code> if and only if the directory was created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *          along with all necessary parent directories; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *          method does not permit verification of the existence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *          named directory and all necessary parent directories; or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *          the <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *          method does not permit the named directory and all necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *          parent directories to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public boolean mkdirs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        if (exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        if (mkdir()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        File canonFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            canonFile = getCanonicalFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        File parent = canonFile.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return (parent != null && (parent.mkdirs() || parent.exists()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                canonFile.mkdir());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * Renames the file denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * <p> Many aspects of the behavior of this method are inherently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * platform-dependent: The rename operation might not be able to move a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * file from one filesystem to another, it might not be atomic, and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * might not succeed if a file with the destination abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * already exists.  The return value should always be checked to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * that the rename operation was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @param  dest  The new abstract pathname for the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @return  <code>true</code> if and only if the renaming succeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *          method denies write access to either the old or new pathnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *          If parameter <code>dest</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    public boolean renameTo(File dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            security.checkWrite(dest.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        return fs.rename(this, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * Sets the last-modified time of the file or directory named by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <p> All platforms support file-modification times to the nearest second,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * but some provide more precision.  The argument will be truncated to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * the supported precision.  If the operation succeeds and no intervening
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * operations on the file take place, then the next invocation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * <code>{@link #lastModified}</code> method will return the (possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * truncated) <code>time</code> argument that was passed to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @param  time  The new last-modified time, measured in milliseconds since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *               the epoch (00:00:00 GMT, January 1, 1970)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * @return <code>true</code> if and only if the operation succeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @throws  IllegalArgumentException  If the argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    public boolean setLastModified(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        if (time < 0) throw new IllegalArgumentException("Negative time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        return fs.setLastModifiedTime(this, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * Marks the file or directory named by this abstract pathname so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * only read operations are allowed.  After invoking this method the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * or directory is guaranteed not to change until it is either deleted or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * marked to allow write access.  Whether or not a read-only file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * directory may be deleted depends upon the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @return <code>true</code> if and only if the operation succeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    public boolean setReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        return fs.setReadOnly(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Sets the owner's or everybody's write permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @param   writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *          If <code>true</code>, sets the access permission to allow write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *          operations; if <code>false</code> to disallow write operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @param   ownerOnly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *          If <code>true</code>, the write permission applies only to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *          owner's write permission; otherwise, it applies to everybody.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *          the underlying file system can not distinguish the owner's write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *          permission from that of others, then the permission will apply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *          everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * @return  <code>true</code> if and only if the operation succeeded. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *          operation will fail if the user does not have permission to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *          the access permissions of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    public boolean setWritable(boolean writable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        return fs.setPermission(this, FileSystem.ACCESS_WRITE, writable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * A convenience method to set the owner's write permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * <p> An invocation of this method of the form <tt>file.setWritable(arg)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *     file.setWritable(arg, true) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * @param   writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *          If <code>true</code>, sets the access permission to allow write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *          operations; if <code>false</code> to disallow write operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * @return  <code>true</code> if and only if the operation succeeded.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *          change the access permissions of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    public boolean setWritable(boolean writable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        return setWritable(writable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * Sets the owner's or everybody's read permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @param   readable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *          If <code>true</code>, sets the access permission to allow read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     *          operations; if <code>false</code> to disallow read operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * @param   ownerOnly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *          If <code>true</code>, the read permission applies only to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     *          owner's read permission; otherwise, it applies to everybody.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     *          the underlying file system can not distinguish the owner's read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     *          permission from that of others, then the permission will apply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *          everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * @return  <code>true</code> if and only if the operation succeeded.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *          change the access permissions of this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *          <code>readable</code> is <code>false</code> and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *          file system does not implement a read permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    public boolean setReadable(boolean readable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        return fs.setPermission(this, FileSystem.ACCESS_READ, readable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * A convenience method to set the owner's read permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * <p>An invocation of this method of the form <tt>file.setReadable(arg)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *     file.setReadable(arg, true) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * @param  readable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *          If <code>true</code>, sets the access permission to allow read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *          operations; if <code>false</code> to disallow read operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @return  <code>true</code> if and only if the operation succeeded.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *          change the access permissions of this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *          <code>readable</code> is <code>false</code> and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *          file system does not implement a read permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    public boolean setReadable(boolean readable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        return setReadable(readable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * Sets the owner's or everybody's execute permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @param   executable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *          If <code>true</code>, sets the access permission to allow execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     *          operations; if <code>false</code> to disallow execute operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * @param   ownerOnly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *          If <code>true</code>, the execute permission applies only to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *          owner's execute permission; otherwise, it applies to everybody.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *          If the underlying file system can not distinguish the owner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *          execute permission from that of others, then the permission will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *          apply to everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @return  <code>true</code> if and only if the operation succeeded.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *          change the access permissions of this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *          <code>executable</code> is <code>false</code> and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *          file system does not implement an execute permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    public boolean setExecutable(boolean executable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        return fs.setPermission(this, FileSystem.ACCESS_EXECUTE, executable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * A convenience method to set the owner's execute permission for this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * <p>An invocation of this method of the form <tt>file.setExcutable(arg)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *     file.setExecutable(arg, true) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @param   executable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     *          If <code>true</code>, sets the access permission to allow execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *          operations; if <code>false</code> to disallow execute operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * @return   <code>true</code> if and only if the operation succeeded.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     *           operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *           change the access permissions of this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *           <code>executable</code> is <code>false</code> and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *           file system does not implement an excute permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *           operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    public boolean setExecutable(boolean executable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return setExecutable(executable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * Tests whether the application can execute the file denoted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @return  <code>true</code> if and only if the abstract pathname exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     *          <em>and</em> the application is allowed to execute the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *          java.lang.SecurityManager#checkExec(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *          method denies execute access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    public boolean canExecute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            security.checkExec(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        return fs.checkAccess(this, FileSystem.ACCESS_EXECUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    /* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * List the available filesystem roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * <p> A particular Java platform may support zero or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * hierarchically-organized file systems.  Each file system has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * {@code root} directory from which all other files in that file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * can be reached.  Windows platforms, for example, have a root directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * for each active drive; UNIX platforms have a single root directory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * namely {@code "/"}.  The set of available filesystem roots is affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * by various system-level operations such as the insertion or ejection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * removable media and the disconnecting or unmounting of physical or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * virtual disk drives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * <p> This method returns an array of {@code File} objects that denote the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * root directories of the available filesystem roots.  It is guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * that the canonical pathname of any file physically present on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * machine will begin with one of the roots returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * <p> The canonical pathname of a file that resides on some other machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * and is accessed via a remote-filesystem protocol such as SMB or NFS may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * or may not begin with one of the roots returned by this method.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * pathname of a remote file is syntactically indistinguishable from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * pathname of a local file then it will begin with one of the roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * returned by this method.  Thus, for example, {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * denoting the root directories of the mapped network drives of a Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * platform will be returned by this method, while {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * containing UNC pathnames will not be returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * <p> Unlike most methods in this class, this method does not throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * security exceptions.  If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * SecurityManager#checkRead(String)} method denies read access to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * particular root directory, then that directory will not appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @return  An array of {@code File} objects denoting the available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     *          filesystem roots, or {@code null} if the set of roots could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *          be determined.  The array will be empty if there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *          filesystem roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    public static File[] listRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        return fs.listRoots();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    /* -- Disk usage -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * Returns the size of the partition <a href="#partName">named</a> by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * @return  The size, in bytes, of the partition or <tt>0L</tt> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     *          abstract pathname does not name a partition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    public long getTotalSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        return fs.getSpace(this, FileSystem.SPACE_TOTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * Returns the number of unallocated bytes in the partition <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * href="#partName">named</a> by this abstract path name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * <p> The returned number of unallocated bytes is a hint, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * a guarantee, that it is possible to use most or any of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * bytes.  The number of unallocated bytes is most likely to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * accurate immediately after this call.  It is likely to be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * inaccurate by any external I/O operations including those made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * on the system outside of this virtual machine.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * makes no guarantee that write operations to this file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @return  The number of unallocated bytes on the partition <tt>0L</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     *          if the abstract pathname does not name a partition.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *          value will be less than or equal to the total file system size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *          returned by {@link #getTotalSpace}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    public long getFreeSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        return fs.getSpace(this, FileSystem.SPACE_FREE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * Returns the number of bytes available to this virtual machine on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * partition <a href="#partName">named</a> by this abstract pathname.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * possible, this method checks for write permissions and other operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * system restrictions and will therefore usually provide a more accurate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * estimate of how much new data can actually be written than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * #getFreeSpace}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * <p> The returned number of available bytes is a hint, but not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * guarantee, that it is possible to use most or any of these bytes.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * number of unallocated bytes is most likely to be accurate immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * after this call.  It is likely to be made inaccurate by any external
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * I/O operations including those made on the system outside of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * virtual machine.  This method makes no guarantee that write operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * to this file system will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @return  The number of available bytes on the partition or <tt>0L</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     *          if the abstract pathname does not name a partition.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *          systems where this information is not available, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *          will be equivalent to a call to {@link #getFreeSpace}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    public long getUsableSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        return fs.getSpace(this, FileSystem.SPACE_USABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    /* -- Temporary files -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    private static final Object tmpFileLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    private static int counter = -1; /* Protected by tmpFileLock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    private static File generateFile(String prefix, String suffix, File dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        if (counter == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            counter = new Random().nextInt() & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        return new File(dir, prefix + Integer.toString(counter) + suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    private static String tmpdir; /* Protected by tmpFileLock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    private static String getTempDir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        if (tmpdir == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            tmpdir = fs.normalize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    new GetPropertyAction("java.io.tmpdir")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        return tmpdir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    private static boolean checkAndCreate(String filename, SecurityManager sm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                sm.checkWrite(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            } catch (AccessControlException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                /* Throwing the original AccessControlException could disclose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                   the location of the default temporary directory, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                   re-throw a more innocuous SecurityException */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                throw new SecurityException("Unable to create temporary file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        return fs.createFileExclusively(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * <p> Creates a new empty file in the specified directory, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * given prefix and suffix strings to generate its name.  If this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * returns successfully then it is guaranteed that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * <li> The file denoted by the returned abstract pathname did not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     *      before this method was invoked, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * <li> Neither this method nor any of its variants will return the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     *      abstract pathname again in the current invocation of the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     *      machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * This method provides only part of a temporary-file facility.  To arrange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * for a file created by this method to be deleted automatically, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * <code>{@link #deleteOnExit}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * <p> The <code>prefix</code> argument must be at least three characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * long.  It is recommended that the prefix be a short, meaningful string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * such as <code>"hjb"</code> or <code>"mail"</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * <code>suffix</code> argument may be <code>null</code>, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * suffix <code>".tmp"</code> will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * <p> To create the new file, the prefix and the suffix may first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * adjusted to fit the limitations of the underlying platform.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * prefix is too long then it will be truncated, but its first three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * characters will always be preserved.  If the suffix is too long then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * too will be truncated, but if it begins with a period character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * (<code>'.'</code>) then the period and the first three characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * following it will always be preserved.  Once these adjustments have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * made the name of the new file will be generated by concatenating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * prefix, five or more internally-generated characters, and the suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * <p> If the <code>directory</code> argument is <code>null</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * system-dependent default temporary-file directory will be used.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * default temporary-file directory is specified by the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * <code>java.io.tmpdir</code>.  On UNIX systems the default value of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * property is typically <code>"/tmp"</code> or <code>"/var/tmp"</code>; on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * Microsoft Windows systems it is typically <code>"C:\\WINNT\\TEMP"</code>.  A different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * value may be given to this system property when the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * is invoked, but programmatic changes to this property are not guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * to have any effect upon the temporary directory used by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * @param  prefix     The prefix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     *                    name; must be at least three characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * @param  suffix     The suffix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *                    name; may be <code>null</code>, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     *                    suffix <code>".tmp"</code> will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * @param  directory  The directory in which the file is to be created, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     *                    <code>null</code> if the default temporary-file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *                    directory is to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @return  An abstract pathname denoting a newly-created empty file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *          If the <code>prefix</code> argument contains fewer than three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *          characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * @throws  IOException  If a file could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *          method does not allow a file to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    public static File createTempFile(String prefix, String suffix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                                      File directory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        if (prefix == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        if (prefix.length() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            throw new IllegalArgumentException("Prefix string too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        String s = (suffix == null) ? ".tmp" : suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        synchronized (tmpFileLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            if (directory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                String tmpDir = getTempDir();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                directory = new File(tmpDir, fs.prefixLength(tmpDir));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            File f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                f = generateFile(prefix, s, directory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            } while (!checkAndCreate(f.getPath(), sm));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * Creates an empty file in the default temporary-file directory, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * the given prefix and suffix to generate its name.  Invoking this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * is equivalent to invoking <code>{@link #createTempFile(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * java.lang.String, java.io.File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * createTempFile(prefix,&nbsp;suffix,&nbsp;null)}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @param  prefix     The prefix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     *                    name; must be at least three characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * @param  suffix     The suffix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     *                    name; may be <code>null</code>, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     *                    suffix <code>".tmp"</code> will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * @return  An abstract pathname denoting a newly-created empty file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     *          If the <code>prefix</code> argument contains fewer than three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     *          characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * @throws  IOException  If a file could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     *          If a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     *          method does not allow a file to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    public static File createTempFile(String prefix, String suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        return createTempFile(prefix, suffix, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    /* -- Basic infrastructure -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * Compares two abstract pathnames lexicographically.  The ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * defined by this method depends upon the underlying system.  On UNIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * systems it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * @param   pathname  The abstract pathname to be compared to this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *                    pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * @return  Zero if the argument is equal to this abstract pathname, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     *          value less than zero if this abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *          lexicographically less than the argument, or a value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     *          than zero if this abstract pathname is lexicographically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *          greater than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    public int compareTo(File pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        return fs.compare(this, pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * Tests this abstract pathname for equality with the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * Returns <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * <code>null</code> and is an abstract pathname that denotes the same file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * or directory as this abstract pathname.  Whether or not two abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * pathnames are equal depends upon the underlying system.  On UNIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * systems it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * @param   obj   The object to be compared with this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * @return  <code>true</code> if and only if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        if ((obj != null) && (obj instanceof File)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            return compareTo((File)obj) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * Computes a hash code for this abstract pathname.  Because equality of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * abstract pathnames is inherently system-dependent, so is the computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * of their hash codes.  On UNIX systems, the hash code of an abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * pathname is equal to the exclusive <em>or</em> of the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * of its pathname string and the decimal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * <code>1234321</code>.  On Microsoft Windows systems, the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * code is equal to the exclusive <em>or</em> of the hash code of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * its pathname string converted to lower case and the decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * value <code>1234321</code>.  Locale is not taken into account on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * lowercasing the pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @return  A hash code for this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        return fs.hashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * Returns the pathname string of this abstract pathname.  This is just the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * string returned by the <code>{@link #getPath}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * @return  The string form of this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        return getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * WriteObject is called to save this filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * The separator character is saved also so it can be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * in case the path is reconstituted on a different host type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @serialData  Default fields followed by separator character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        s.writeChar(this.separatorChar); // Add the separator character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * readObject is called to restore this filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * The original separator character is read.  If it is different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * than the separator character on this system, then the old separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * is replaced by the local separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        char sep = s.readChar(); // read the previous separator char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        if (sep != separatorChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            this.path = this.path.replace(sep, separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        this.path = fs.normalize(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    private static final long serialVersionUID = 301077366599181567L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    // Set up JavaIODeleteOnExitAccess in SharedSecrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    // Added here as DeleteOnExitHook is package-private and SharedSecrets cannot easily access it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        sun.misc.SharedSecrets.setJavaIODeleteOnExitAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            new sun.misc.JavaIODeleteOnExitAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    DeleteOnExitHook.hook().run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
}