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