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 * | 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> 1066 * Once deletion has been requested, it may be cancelled by invoking 1067 * {@link #cancelDeleteOnExit()}. 1068 * 1069 * <P> 1070 * Note: this method should <i>not</i> be used for file-locking, as 1071 * the resulting protocol cannot be made to work reliably. The 1072 * {@link java.nio.channels.FileLock FileLock} 1073 * facility should be used instead. 1074 * 1075 * @throws SecurityException 1076 * If a security manager exists and its {@link 1077 * java.lang.SecurityManager#checkDelete} method denies 1078 * delete access to the file 1079 * 1080 * @see #cancelDeleteOnExit 1081 * @see #delete 1082 * 1083 * @since 1.2 1084 */ 1085 public void deleteOnExit() { 1086 SecurityManager security = System.getSecurityManager(); 1087 if (security != null) { 1088 security.checkDelete(path); 1089 } 1090 if (isInvalid()) { 1091 return; 1092 } 1093 DeleteOnExitHook.add(path); 1094 } 1095 1096 /** 1097 * Cancels any request that the file or directory denoted by this 1098 * abstract pathname be deleted when the virtual machine terminates. 1099 * Invoking this method for a file or directory that is not already 1100 * registered for deletion has no effect. If a file or directory is 1101 * registered for deletion but is explicitly deleted before normal 1102 * termination of the virtual machine, then it is recommended to call 1103 * this method to free resources used to track the file for deletion. 1104 * 1105 * @see #deleteOnExit 1106 * 1107 * @since 14 1108 */ 1109 public void cancelDeleteOnExit() { 1110 if (isInvalid()) { 1111 return; 1112 } 1113 DeleteOnExitHook.remove(path); 1114 } 1115 1116 /** 1117 * Returns an array of strings naming the files and directories in the 1118 * directory denoted by this abstract pathname. 1119 * 1120 * <p> If this abstract pathname does not denote a directory, then this 1121 * method returns {@code null}. Otherwise an array of strings is 1122 * returned, one for each file or directory in the directory. Names 1123 * denoting the directory itself and the directory's parent directory are 1124 * not included in the result. Each string is a file name rather than a 1125 * complete path. 1126 * 1127 * <p> There is no guarantee that the name strings in the resulting array 1128 * will appear in any specific order; they are not, in particular, 1129 * guaranteed to appear in alphabetical order. 1130 * 1131 * <p> Note that the {@link java.nio.file.Files} class defines the {@link 1132 * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method to 1133 * open a directory and iterate over the names of the files in the directory. 1134 * This may use less resources when working with very large directories, and 1135 * may be more responsive when working with remote directories. 1136 * |