OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 * The modes in which a File can be opened. | 8 * The modes in which a File can be opened. |
9 */ | 9 */ |
10 class FileMode { | 10 class FileMode { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 * A more flexible and useful way to read a file is with a [Stream]. | 91 * A more flexible and useful way to read a file is with a [Stream]. |
92 * Open the file with [openRead], which returns a stream that | 92 * Open the file with [openRead], which returns a stream that |
93 * provides the data in the file as chunks of bytes. | 93 * provides the data in the file as chunks of bytes. |
94 * Listen to the stream for data and process as needed. | 94 * Listen to the stream for data and process as needed. |
95 * You can use various transformers in succession to manipulate the | 95 * You can use various transformers in succession to manipulate the |
96 * data into the required format or to prepare it for output. | 96 * data into the required format or to prepare it for output. |
97 * | 97 * |
98 * You might want to use a stream to read large files, | 98 * You might want to use a stream to read large files, |
99 * to manipulate the data with tranformers, | 99 * to manipulate the data with tranformers, |
100 * or for compatibility with another API, such as [WebSocket]s. | 100 * or for compatibility with another API, such as [WebSocket]s. |
101 * | 101 * |
102 * import 'dart:io'; | 102 * import 'dart:io'; |
103 * import 'dart:convert'; | 103 * import 'dart:convert'; |
104 * import 'dart:async'; | 104 * import 'dart:async'; |
105 * | 105 * |
106 * main() { | 106 * main() { |
107 * final file = new File('file.txt'); | 107 * final file = new File('file.txt'); |
108 * Stream<List<int>> inputStream = file.openRead(); | 108 * Stream<List<int>> inputStream = file.openRead(); |
109 * | 109 * |
110 * inputStream | 110 * inputStream |
111 * .transform(UTF8.decoder) // Decode bytes to UTF8. | 111 * .transform(UTF8.decoder) // Decode bytes to UTF8. |
(...skipping 17 matching lines...) Expand all Loading... |
129 * .then((File file) { | 129 * .then((File file) { |
130 * // Do something with the file. | 130 * // Do something with the file. |
131 * }); | 131 * }); |
132 * } | 132 * } |
133 * | 133 * |
134 * You can also write to a file using a [Stream]. Open the file with | 134 * You can also write to a file using a [Stream]. Open the file with |
135 * [openWrite], which returns a stream to which you can write data. | 135 * [openWrite], which returns a stream to which you can write data. |
136 * Be sure to close the file with the [close] method. | 136 * Be sure to close the file with the [close] method. |
137 * | 137 * |
138 * import 'dart:io'; | 138 * import 'dart:io'; |
139 * | 139 * |
140 * void main() { | 140 * void main() { |
141 * var file = new File('file.txt'); | 141 * var file = new File('file.txt'); |
142 * var sink = file.openWrite(); | 142 * var sink = file.openWrite(); |
143 * sink.write('FILE ACCESSED ${new DateTime.now()}\n'); | 143 * sink.write('FILE ACCESSED ${new DateTime.now()}\n'); |
144 * | 144 * |
145 * // Close the IOSink to free system resources. | 145 * // Close the IOSink to free system resources. |
146 * sink.close(); | 146 * sink.close(); |
147 * } | 147 * } |
148 * | 148 * |
149 * ## The use of Futures | 149 * ## The use of Futures |
150 * | 150 * |
151 * To avoid unintentional blocking of the program, | 151 * To avoid unintentional blocking of the program, |
152 * several methods use a [Future] to return a value. For example, | 152 * several methods use a [Future] to return a value. For example, |
153 * the [length] method, which gets the length of a file, returns a Future. | 153 * the [length] method, which gets the length of a file, returns a Future. |
154 * Use `then` to register a callback function, which is called when | 154 * Use `then` to register a callback function, which is called when |
155 * the value is ready. | 155 * the value is ready. |
156 * | 156 * |
157 * import 'dart:io'; | 157 * import 'dart:io'; |
158 * | 158 * |
159 * main() { | 159 * main() { |
160 * final file = new File('file.txt'); | 160 * final file = new File('file.txt'); |
161 * | 161 * |
162 * file.length().then((len) { | 162 * file.length().then((len) { |
163 * print(len); | 163 * print(len); |
164 * }); | 164 * }); |
165 * } | 165 * } |
166 * | 166 * |
167 * In addition to length, the [exists], [lastModified], [stat], and | 167 * In addition to length, the [exists], [lastModified], [stat], and |
168 * other methods, return Futures. | 168 * other methods, return Futures. |
169 * | 169 * |
170 * ## Other resources | 170 * ## Other resources |
171 * | 171 * |
172 * * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directori
es-and-symlinks) | 172 * * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directori
es-and-symlinks) |
173 * provides additional task-oriented code samples that show how to use | 173 * provides additional task-oriented code samples that show how to use |
174 * various API from the Directory class and the related [File] class. | 174 * various API from the Directory class and the related [File] class. |
175 * | 175 * |
176 * * [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) | 176 * * [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) |
177 * a section from _A Tour of the Dart Libraries_ | 177 * a section from _A Tour of the Dart Libraries_ |
178 * covers files and directories. | 178 * covers files and directories. |
179 * | 179 * |
180 * * [Write Command-Line Apps](https://www.dartlang.org/docs/tutorials/cmdline/)
, | 180 * * [Write Command-Line Apps](https://www.dartlang.org/docs/tutorials/cmdline/)
, |
181 * a tutorial about writing command-line apps, includes information | 181 * a tutorial about writing command-line apps, includes information |
182 * about files and directories. | 182 * about files and directories. |
183 | 183 |
184 */ | 184 */ |
185 abstract class File extends FileSystemEntity { | 185 abstract class File implements FileSystemEntity { |
186 /** | 186 /** |
187 * Creates a [File] object. | 187 * Creates a [File] object. |
188 * | 188 * |
189 * If [path] is a relative path, it will be interpreted relative to the | 189 * If [path] is a relative path, it will be interpreted relative to the |
190 * current working directory (see [Directory.current]), when used. | 190 * current working directory (see [Directory.current]), when used. |
191 * | 191 * |
192 * If [path] is an absolute path, it will be immune to changes to the | 192 * If [path] is an absolute path, it will be immune to changes to the |
193 * current working directory. | 193 * current working directory. |
194 */ | 194 */ |
195 factory File(String path) => new _File(path); | 195 factory File(String path) => new _File(path); |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 sb.write(": $osError"); | 861 sb.write(": $osError"); |
862 if (path != null) { | 862 if (path != null) { |
863 sb.write(", path = '$path'"); | 863 sb.write(", path = '$path'"); |
864 } | 864 } |
865 } else if (path != null) { | 865 } else if (path != null) { |
866 sb.write(": $path"); | 866 sb.write(": $path"); |
867 } | 867 } |
868 return sb.toString(); | 868 return sb.toString(); |
869 } | 869 } |
870 } | 870 } |
OLD | NEW |