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 * FileMode describes 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 { |
11 /// The [FileMode] for opening a file only for reading. | 11 /// The mode for opening a file only for reading. |
12 static const READ = const FileMode._internal(0); | 12 static const READ = const FileMode._internal(0); |
13 /// The [FileMode] for opening a file for reading and writing. The file will | 13 /// The mode for opening a file for reading and writing. The file is |
14 /// be overwritten. If the file does not exist, it will be created. | 14 /// overwritten if it already exists. The file is created if it does not |
| 15 /// already exist. |
15 static const WRITE = const FileMode._internal(1); | 16 static const WRITE = const FileMode._internal(1); |
16 /// The [FileMode] for opening a file for reading a file and writing to the | 17 /// The mode for opening a file for reading and writing to the |
17 /// end of it. If the file does not exist, it will be created. | 18 /// end of it. The file is created if it does not already exist. |
18 static const APPEND = const FileMode._internal(2); | 19 static const APPEND = const FileMode._internal(2); |
19 final int _mode; | 20 final int _mode; |
20 | 21 |
21 const FileMode._internal(this._mode); | 22 const FileMode._internal(this._mode); |
22 } | 23 } |
23 | 24 |
24 /// The [FileMode] for opening a file only for reading. | 25 /// The mode for opening a file only for reading. |
25 const READ = FileMode.READ; | 26 const READ = FileMode.READ; |
26 /// The [FileMode] for opening a file for reading and writing. The file will be | 27 /// The mode for opening a file for reading and writing. The file is |
27 /// overwritten. If the file does not exist, it will be created. | 28 /// overwritten if it already exists. The file is created if it does not |
| 29 /// already exist. |
28 const WRITE = FileMode.WRITE; | 30 const WRITE = FileMode.WRITE; |
29 /// The [FileMode] for opening a file for reading a file and writing to the end | 31 /// The mode for opening a file for reading and writing to the |
30 /// of it. If the file does not exist, it will be created. | 32 /// end of it. The file is created if it does not already exist. |
31 const APPEND = FileMode.APPEND; | 33 const APPEND = FileMode.APPEND; |
32 | 34 |
33 /** | 35 /** |
34 * A reference to a file on the file system. | 36 * A reference to a file on the file system. |
35 * | 37 * |
36 * If [path] is a symbolic link, rather than a file, then | 38 * A File instance is an object that holds a [path] on which operations can |
37 * the methods of [File] operate on the ultimate target of the | 39 * be performed. |
38 * link, except for File.delete and File.deleteSync, which operate on | 40 * You can get the parent directory of the file using the getter [parent], |
| 41 * a property inherited from [FileSystemEntity]. |
| 42 * |
| 43 * Create a new File object with a pathname to access the specified file on the |
| 44 * file system from your program. |
| 45 * |
| 46 * var myFile = new File('file.txt'); |
| 47 * |
| 48 * The File class contains methods for manipulating files and their contents. |
| 49 * Using methods in this class, you can open and close files, read to and write |
| 50 * from them, create and delete them, and check for their existence. |
| 51 * |
| 52 * When reading or writing a file, you can use streams (with [openRead]), |
| 53 * random access operations (with [open]), |
| 54 * or convenience methods such as [readAsString], |
| 55 * |
| 56 * Most methods in this class occur in synchronous and asynchronous pairs, |
| 57 * for example, [readAsString] and [readAsStringSync]. |
| 58 * Unless you have a specific reason for using the synchronous version |
| 59 * of a method, prefer the asynchronous version to avoid blocking your program. |
| 60 * |
| 61 * ## If path is a link |
| 62 * |
| 63 * If [path] is a symbolic link, rather than a file, |
| 64 * then the methods of File operate on the ultimate target of the |
| 65 * link, except for [delete] and [deleteSync], which operate on |
39 * the link. | 66 * the link. |
40 * | 67 * |
41 * To operate on the underlying file data there are two options: | 68 * ## Read from a file |
42 * | 69 * |
43 * * Use streaming: read the contents of the file from the [Stream] | 70 * The following code sample reads the entire contents from a file as a string |
44 * this.[openRead]() and write to the file by writing to the [IOSink] | 71 * using the asynchronous [readAsString] method: |
45 * this.[openWrite](). | 72 * |
46 * * Open the file for random access operations using [open]. | 73 * import 'dart:async'; |
| 74 * import 'dart:io'; |
| 75 * |
| 76 * void main() { |
| 77 * new File('file.txt').readAsString().then((String contents) { |
| 78 * print(contents); |
| 79 * }); |
| 80 * } |
| 81 * |
| 82 * A more flexible and useful way to read a file is with a [Stream]. |
| 83 * Open the file with [openRead], which returns a stream that |
| 84 * provides the data in the file as chunks of bytes. |
| 85 * Listen to the stream for data and process as needed. |
| 86 * You can use various transformers in succession to manipulate the |
| 87 * data into the required format or to prepare it for output. |
| 88 * |
| 89 * You might want to use a stream to read large files, |
| 90 * to manipulate the data with tranformers, |
| 91 * or for compatibility with another API, such as [WebSocket]s. |
| 92 * |
| 93 * import 'dart:io'; |
| 94 * import 'dart:convert'; |
| 95 * import 'dart:async'; |
| 96 * |
| 97 * main() { |
| 98 * final file = new File('file.txt'); |
| 99 * Stream<List<int>> inputStream = file.openRead(); |
| 100 * |
| 101 * inputStream |
| 102 * .transform(UTF8.decoder) // Decode bytes to UTF8. |
| 103 * .transform(new LineSplitter()) // Convert stream to individual lines. |
| 104 * .listen((String line) { // Process results. |
| 105 * print('$line: ${line.length} bytes'); |
| 106 * }, |
| 107 * onDone: () { print('File is now closed.'); }, |
| 108 * onError: (e) { print(e.toString()); }); |
| 109 * } |
| 110 * |
| 111 * ## Write to a file |
| 112 * |
| 113 * To write a string to a file, use the [writeAsString] method: |
| 114 * |
| 115 * import 'dart:io'; |
| 116 * |
| 117 * void main() { |
| 118 * final filename = 'file.txt'; |
| 119 * new File(filename).writeAsString('some content') |
| 120 * .then((File file) { |
| 121 * // Do something with the file. |
| 122 * }); |
| 123 * } |
| 124 * |
| 125 * You can also write to a file using a [Stream]. Open the file with |
| 126 * [openWrite], which returns a stream to which you can write data. |
| 127 * Be sure to close the file with the [close] method. |
| 128 * |
| 129 * import 'dart:io'; |
| 130 * |
| 131 * void main() { |
| 132 * var file = new File('file.txt'); |
| 133 * var sink = file.openWrite(); |
| 134 * sink.write('FILE ACCESSED ${new DateTime.now()}\n'); |
| 135 * |
| 136 * // Close the IOSink to free system resources. |
| 137 * sink.close(); |
| 138 * } |
| 139 * |
| 140 * ## The use of Futures |
| 141 * |
| 142 * To avoid unintentional blocking of the program, |
| 143 * several methods use a [Future] to return a value. For example, |
| 144 * the [length] method, which gets the length of a file, returns a Future. |
| 145 * Use `then` to register a callback function, which is called when |
| 146 * the value is ready. |
| 147 * |
| 148 * import 'dart:io'; |
| 149 * |
| 150 * main() { |
| 151 * final file = new File('file.txt'); |
| 152 * |
| 153 * file.length().then((len) { |
| 154 * print(len); |
| 155 * }); |
| 156 * } |
| 157 * |
| 158 * In addition to length, the [exists], [lastModified], [stat], and |
| 159 * other methods, return Futures. |
| 160 * |
| 161 * ## Other resources |
| 162 * |
| 163 * * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directori
es-and-symlinks) |
| 164 * provides additional task-oriented code samples that show how to use |
| 165 * various API from the Directory class and the related [File] class. |
| 166 * |
| 167 * * [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) |
| 168 * a section from _A Tour of the Dart Libraries_ |
| 169 * covers files and directories. |
| 170 * |
| 171 * * [Write Command-Line Apps](https://www.dartlang.org/docs/tutorials/cmdline/)
, |
| 172 * a tutorial about writing command-line apps, includes information |
| 173 * about files and directories. |
| 174 |
47 */ | 175 */ |
48 abstract class File extends FileSystemEntity { | 176 abstract class File extends FileSystemEntity { |
49 /** | 177 /** |
50 * Create a File object. | 178 * Create a File object. |
51 */ | 179 */ |
52 factory File(String path) => new _File(path); | 180 factory File(String path) => new _File(path); |
53 | 181 |
54 /** | 182 /** |
55 * Create a File object from a URI. | 183 * Create a File object from a URI. |
56 * | 184 * |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 sb.write(": $osError"); | 734 sb.write(": $osError"); |
607 if (path != null) { | 735 if (path != null) { |
608 sb.write(", path = '$path'"); | 736 sb.write(", path = '$path'"); |
609 } | 737 } |
610 } else if (path != null) { | 738 } else if (path != null) { |
611 sb.write(": $path"); | 739 sb.write(": $path"); |
612 } | 740 } |
613 return sb.toString(); | 741 return sb.toString(); |
614 } | 742 } |
615 } | 743 } |
OLD | NEW |