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