OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * A reference to a directory (or _folder_) on the file system. | 8 * A reference to a directory (or _folder_) on the file system. |
| 9 * |
| 10 * A Directory instance is an object holding a [path] on which operations can |
| 11 * be performed. The path to the directory can be [absolute] or [relative]. |
| 12 * You can get the parent directory using the getter [parent], |
| 13 * a property inherited from [FileSystemEntity]. |
| 14 * |
| 15 * In addition to being used as an instance to access the file system, |
| 16 * Directory has a number of static properties, such as [systemTemp], |
| 17 * which gets the system's temporary directory, and the getter and setter |
| 18 * [current], which you can use to access or change the current directory. |
| 19 * |
| 20 * Create a new Directory object with a pathname to access the specified |
| 21 * directory on the file system from your program. |
| 22 * |
| 23 * var myDir = new Directory('myDir'); |
| 24 * |
| 25 * Most methods in this class occur in synchronous and asynchronous pairs, |
| 26 * for example, [create] and [createSync]. |
| 27 * Unless you have a specific reason for using the synchronous version |
| 28 * of a method, prefer the asynchronous version to avoid blocking your program. |
| 29 * |
| 30 * ## Create a directory |
| 31 * |
| 32 * The following code sample creates a directory using the [create] method. |
| 33 * By setting the `recursive` parameter to true, you can create the |
| 34 * named directory and all its necessary parent directories, |
| 35 * if they do not already exist. |
| 36 * |
| 37 * import 'dart:io'; |
| 38 * |
| 39 * void main() { |
| 40 * // Creates dir/ and dir/subdir/. |
| 41 * new Directory('dir/subdir').create(recursive: true) |
| 42 * // The created directory is returned as a Future. |
| 43 * .then((Directory directory) { |
| 44 * print(directory.path); |
| 45 * }); |
| 46 * } |
| 47 * |
| 48 * ## List a directory |
| 49 * |
| 50 * Use the [list] or [listSync] methods to get the files and directories |
| 51 * contained by a directory. |
| 52 * Set `recursive` to true to recursively list all subdirectories. |
| 53 * Set `followLinks` to true to follow symbolic links. |
| 54 * The list method returns a [Stream] that provides FileSystemEntity |
| 55 * objects. Use the listen callback function to process each object |
| 56 * as it become available. |
| 57 * |
| 58 * import 'dart:io'; |
| 59 * |
| 60 * void main() { |
| 61 * // Get the system temp directory. |
| 62 * var systemTempDir = Directory.systemTemp; |
| 63 * |
| 64 * // List directory contents, recursing into sub-directories, |
| 65 * // but not following symbolic links. |
| 66 * systemTempDir.list(recursive: true, followLinks: false) |
| 67 * .listen((FileSystemEntity entity) { |
| 68 * print(entity.path); |
| 69 * }); |
| 70 * } |
| 71 * |
| 72 * ## The use of Futures |
| 73 * |
| 74 * I/O operations can block a program for some period of time while it waits for |
| 75 * the operation to complete. To avoid this, all |
| 76 * methods involving I/O have an asynchronous variant which returns a [Future]. |
| 77 * This future completes when the I/O operation finishes. While the I/O |
| 78 * operation is in progress, the Dart program is not blocked, |
| 79 * and can perform other operations. |
| 80 * |
| 81 * For example, |
| 82 * the [exists] method, which determines whether the directory exists, |
| 83 * returns a boolean value using a Future. |
| 84 * Use `then` to register a callback function, which is called when |
| 85 * the value is ready. |
| 86 * |
| 87 * import 'dart:io'; |
| 88 * |
| 89 * main() { |
| 90 * final myDir = new Directory('dir'); |
| 91 * myDir.exists().then((isThere) { |
| 92 * isThere ? print('exists') : print('non-existent'); |
| 93 * }); |
| 94 * } |
| 95 * |
| 96 * |
| 97 * In addition to exists, the [stat], [rename], and |
| 98 * other methods, return Futures. |
| 99 * |
| 100 * ## Other resources |
| 101 * |
| 102 * * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directori
es-and-symlinks) |
| 103 * provides additional task-oriented code samples that show how to use |
| 104 * various API from the Directory class and the related [File] class. |
| 105 * |
| 106 * * [I/O for Command-Line Apps](https://www.dartlang.org/docs/dart-up-and-runni
ng/contents/ch03.html#ch03-dartio---file-and-socket-io-for-command-line-apps) |
| 107 * a section from _A Tour of the Dart Libraries_ |
| 108 * covers files and directories. |
| 109 * |
| 110 * * [Write Command-Line Apps](https://www.dartlang.org/docs/tutorials/cmdline/)
, |
| 111 * a tutorial about writing command-line apps, includes information |
| 112 * about files and directories. |
9 */ | 113 */ |
10 abstract class Directory extends FileSystemEntity { | 114 abstract class Directory extends FileSystemEntity { |
11 /** | 115 /** |
12 * Gets the path of this directory. | 116 * Gets the path of this directory. |
13 */ | 117 */ |
14 final String path; | 118 final String path; |
15 | 119 |
16 /** | 120 /** |
17 * Creates a directory object. The path is either an absolute path, | 121 * Creates a directory object. The path is either an absolute path, |
18 * or it is a relative path which is interpreted relative to the directory | 122 * or it is a relative path which is interpreted relative to the directory |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 * directories, files, and links. | 291 * directories, files, and links. |
188 */ | 292 */ |
189 List<FileSystemEntity> listSync({bool recursive: false, | 293 List<FileSystemEntity> listSync({bool recursive: false, |
190 bool followLinks: true}); | 294 bool followLinks: true}); |
191 | 295 |
192 /** | 296 /** |
193 * Returns a human readable string for this Directory instance. | 297 * Returns a human readable string for this Directory instance. |
194 */ | 298 */ |
195 String toString(); | 299 String toString(); |
196 } | 300 } |
OLD | NEW |