< prev index next >

src/java.base/share/classes/java/io/File.java

Print this page




1041      *          If a security manager exists and its {@link
1042      *          java.lang.SecurityManager#checkDelete} method denies
1043      *          delete access to the file
1044      */
1045     public boolean delete() {
1046         SecurityManager security = System.getSecurityManager();
1047         if (security != null) {
1048             security.checkDelete(path);
1049         }
1050         if (isInvalid()) {
1051             return false;
1052         }
1053         return fs.delete(this);
1054     }
1055 
1056     /**
1057      * Requests that the file or directory denoted by this abstract
1058      * pathname be deleted when the virtual machine terminates.
1059      * Files (or directories) are deleted in the reverse order that
1060      * they are registered. Invoking this method to delete a file or
1061      * directory that is already registered for deletion has no effect.

1062      * Deletion will be attempted only for normal termination of the
1063      * virtual machine, as defined by the Java Language Specification.
1064      *
1065      * <p> Once deletion has been requested, it is not possible to cancel the
1066      * request.  This method should therefore be used with care.




1067      *
1068      * <P>
1069      * Note: this method should <i>not</i> be used for file-locking, as
1070      * the resulting protocol cannot be made to work reliably. The
1071      * {@link java.nio.channels.FileLock FileLock}
1072      * facility should be used instead.
1073      *
1074      * @throws  SecurityException
1075      *          If a security manager exists and its {@link
1076      *          java.lang.SecurityManager#checkDelete} method denies
1077      *          delete access to the file
1078      *

1079      * @see #delete
1080      *
1081      * @since 1.2
1082      */
1083     public void deleteOnExit() {
1084         SecurityManager security = System.getSecurityManager();
1085         if (security != null) {
1086             security.checkDelete(path);
1087         }
1088         if (isInvalid()) {
1089             return;
1090         }
1091         DeleteOnExitHook.add(path);
1092     }
1093 
1094     /**


































1095      * Returns an array of strings naming the files and directories in the
1096      * directory denoted by this abstract pathname.
1097      *
1098      * <p> If this abstract pathname does not denote a directory, then this
1099      * method returns {@code null}.  Otherwise an array of strings is
1100      * returned, one for each file or directory in the directory.  Names
1101      * denoting the directory itself and the directory's parent directory are
1102      * not included in the result.  Each string is a file name rather than a
1103      * complete path.
1104      *
1105      * <p> There is no guarantee that the name strings in the resulting array
1106      * will appear in any specific order; they are not, in particular,
1107      * guaranteed to appear in alphabetical order.
1108      *
1109      * <p> Note that the {@link java.nio.file.Files} class defines the {@link
1110      * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method to
1111      * open a directory and iterate over the names of the files in the directory.
1112      * This may use less resources when working with very large directories, and
1113      * may be more responsive when working with remote directories.
1114      *




1041      *          If a security manager exists and its {@link
1042      *          java.lang.SecurityManager#checkDelete} method denies
1043      *          delete access to the file
1044      */
1045     public boolean delete() {
1046         SecurityManager security = System.getSecurityManager();
1047         if (security != null) {
1048             security.checkDelete(path);
1049         }
1050         if (isInvalid()) {
1051             return false;
1052         }
1053         return fs.delete(this);
1054     }
1055 
1056     /**
1057      * Requests that the file or directory denoted by this abstract
1058      * pathname be deleted when the virtual machine terminates.
1059      * Files (or directories) are deleted in the reverse order that
1060      * they are registered. Invoking this method to delete a file or
1061      * directory that is already registered for deletion causes its
1062      * deletion registration reference count to be incremented.
1063      * Deletion will be attempted only for normal termination of the
1064      * virtual machine, as defined by the Java Language Specification.
1065      *
1066      * <p>
1067      * An invocation of this method may be cancelled by invoking
1068      * {@link #cancelDeleteOnExit()}. There must be at least as many
1069      * cancellation as deletion requests however to unregister the file
1070      * or directory completely from eventual deletion. Cancellation does
1071      * not affect the order of deletion.
1072      *
1073      * <P>
1074      * Note: this method should <i>not</i> be used for file-locking, as
1075      * the resulting protocol cannot be made to work reliably. The
1076      * {@link java.nio.channels.FileLock FileLock}
1077      * facility should be used instead.
1078      *
1079      * @throws  SecurityException
1080      *          If a security manager exists and its {@link
1081      *          java.lang.SecurityManager#checkDelete} method denies
1082      *          delete access to the file
1083      *
1084      * @see #cancelDeleteOnExit
1085      * @see #delete
1086      *
1087      * @since 1.2
1088      */
1089     public void deleteOnExit() {
1090         SecurityManager security = System.getSecurityManager();
1091         if (security != null) {
1092             security.checkDelete(path);
1093         }
1094         if (isInvalid()) {
1095             return;
1096         }
1097         DeleteOnExitHook.add(path);
1098     }
1099 
1100     /**
1101      * Cancels a request that the file or directory denoted by this abstract
1102      * pathname be deleted when the virtual machine terminates. Invoking this
1103      * method for a file or directory that is not already registered for
1104      * deletion has no effect. This method will cause the deletion registration
1105      * reference count of a registered file or directory to be decremented but
1106      * will not unregister it unless that count reaches zero.
1107      *
1108      * <p>
1109      * If a file or directory is registered for deletion but is explicitly
1110      * deleted before normal termination of the virtual machine, then it is
1111      * recommended to call this method to free resources used to track the
1112      * file for deletion.
1113      *
1114      * @throws  SecurityException
1115      *          If a security manager exists and its {@link
1116      *          java.lang.SecurityManager#checkDelete} method denies
1117      *          delete access to the file
1118      *
1119      * @see #deleteOnExit
1120      *
1121      * @since 14
1122      */
1123     public void cancelDeleteOnExit() {
1124         SecurityManager security = System.getSecurityManager();
1125         if (security != null) {
1126             security.checkDelete(path);
1127         }
1128         if (isInvalid()) {
1129             return;
1130         }
1131         DeleteOnExitHook.remove(path);
1132     }
1133 
1134     /**
1135      * Returns an array of strings naming the files and directories in the
1136      * directory denoted by this abstract pathname.
1137      *
1138      * <p> If this abstract pathname does not denote a directory, then this
1139      * method returns {@code null}.  Otherwise an array of strings is
1140      * returned, one for each file or directory in the directory.  Names
1141      * denoting the directory itself and the directory's parent directory are
1142      * not included in the result.  Each string is a file name rather than a
1143      * complete path.
1144      *
1145      * <p> There is no guarantee that the name strings in the resulting array
1146      * will appear in any specific order; they are not, in particular,
1147      * guaranteed to appear in alphabetical order.
1148      *
1149      * <p> Note that the {@link java.nio.file.Files} class defines the {@link
1150      * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method to
1151      * open a directory and iterate over the names of the files in the directory.
1152      * This may use less resources when working with very large directories, and
1153      * may be more responsive when working with remote directories.
1154      *


< prev index next >