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] has a [path] (the pathname to the file) and a [parent] (the |
Søren Gjesse
2014/02/12 13:16:35
Like for Directory we should be precise about the
mem
2014/02/12 19:17:07
Done.
| |
37 * the methods of [File] operate on the ultimate target of the | 39 * directory that contains the file). |
38 * link, except for File.delete and File.deleteSync, which operate on | 40 * Create a new File object with a pathname to access the file on the |
41 * file system from your program. | |
42 * | |
43 * var myFile = new File('file.txt'); | |
44 * | |
45 * The File class contains methods for manipulating files and their contents. | |
46 * Using methods in this class, you can open and close files, read to and write | |
47 * from them, create and delete them, and check for their existence. | |
48 * | |
49 * When reading or writing a file, you can use streams (with [openRead]), | |
50 * random access operations (with [open]), | |
51 * or convenience methods such as [readAsString], | |
52 * | |
53 * Most methods in this class occur in synchronous and asynchronous pairs, | |
54 * for example, [readAsString] and [readAsStringSync]. | |
55 * Unless you have a specific reason for using the synchronous version | |
56 * of a method, prefer the asynchronous version to avoid blocking your program. | |
57 * | |
58 * ## If path is a link | |
59 * | |
60 * If [path] is a symbolic link, rather than a file, | |
61 * then the methods of File operate on the ultimate target of the | |
62 * link, except for [delete] and [deleteSync], which operate on | |
39 * the link. | 63 * the link. |
40 * | 64 * |
41 * To operate on the underlying file data there are two options: | 65 * ## Read from a file |
42 * | 66 * |
43 * * Use streaming: read the contents of the file from the [Stream] | 67 * 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] | 68 * using the asynchronous [readAsString] method: |
45 * this.[openWrite](). | 69 * |
46 * * Open the file for random access operations using [open]. | 70 * import 'dart:async'; |
71 * import 'dart:io'; | |
72 * | |
73 * void main() { | |
74 * new File('file.txt').readAsString().then((String contents) { | |
75 * print(contents); | |
76 * }); | |
77 * } | |
78 * | |
79 * A more flexible and useful way to read a file is with a [Stream]. | |
80 * Open the file with [openRead], which returns a stream that reads the file as | |
Søren Gjesse
2014/02/12 13:16:35
stream that ... ->
stream that provides the data
mem
2014/02/12 19:17:07
Done.
| |
81 * bytes. Listen to the stream for data and process as needed. | |
82 * You can use various transformers in succession to manipulate the | |
83 * data into the required format or prepare it for output. | |
84 * | |
85 * You might want to use a stream to read large files, | |
86 * to manipulate the data with tranformers, | |
87 * or for compatibility with another API, such as [WebSocket]s. | |
88 * | |
89 * import 'dart:io'; | |
90 * import 'dart:convert'; | |
91 * import 'dart:async'; | |
92 * | |
93 * main() { | |
94 * final file = new File('file.txt'); | |
95 * Stream<List<int>> inputStream = file.openRead(); | |
96 * | |
97 * inputStream | |
98 * .transform(UTF8.decoder) // Decode bytes to UTF8. | |
99 * .transform(new LineSplitter()) // Convert stream to individual lines. | |
100 * .listen((String line) { // Process results. | |
101 * print('$line: ${line.length} bytes'); | |
102 * }, | |
103 * onDone: () { print('File is now closed.'); }, | |
104 * onError: (e) { print(e.toString()); }); | |
105 * } | |
106 * | |
107 * ## Write to a file | |
108 * | |
109 * To write a string to a file, use the [writeAsString] method: | |
110 * | |
111 * import 'dart:io'; | |
112 * | |
113 * void main() { | |
114 * final filename = 'file.txt'; | |
115 * new File(filename).writeAsString('some content') | |
116 * .then((File file) { | |
117 * // Do something with the file. | |
118 * }); | |
119 * } | |
120 * | |
121 * You can also write to a file using a [Stream]. Open the file with | |
122 * [openWrite], which returns a stream to which you can write data. | |
123 * Be sure to close the file with the [close] method. | |
124 * | |
125 * import 'dart:io'; | |
126 * | |
127 * void main() { | |
128 * var file = new File('file.txt'); | |
129 * var sink = file.openWrite(); | |
130 * sink.write('FILE ACCESSED ${new DateTime.now()}\n'); | |
131 * | |
132 * // Close the IOSink to free system resources. | |
133 * sink.close(); | |
134 * } | |
135 * | |
136 * ## The use of Futures | |
137 * | |
138 * To avoid unintentional blocking of the program, | |
139 * several methods use a [Future] to return a value. For example, | |
140 * the [length] method, which gets the length of a file, returns a Future. | |
141 * Use `then` to register a callback function, which is called when | |
142 * the value is ready. | |
143 * | |
144 * import 'dart:io'; | |
145 * | |
146 * main() { | |
147 * final file = new File('file.txt'); | |
148 * | |
149 * file.length().then((len) { | |
150 * print(len); | |
151 * }); | |
152 * } | |
153 * | |
154 * In addition to length, the [exists], [lastModified], [stat], and | |
155 * other methods, return Futures. | |
156 * | |
157 * ## Other resources | |
158 * | |
159 * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directories -and-symlinks) | |
160 * provides additional task-oriented code samples that show how to use | |
161 * various API from the File class and the related [Directory] class. | |
47 */ | 162 */ |
48 abstract class File implements FileSystemEntity { | 163 abstract class File implements FileSystemEntity { |
49 /** | 164 /** |
50 * Create a File object. | 165 * Create a File object. |
51 */ | 166 */ |
52 factory File(String path) => new _File(path); | 167 factory File(String path) => new _File(path); |
53 | 168 |
54 /** | 169 /** |
55 * Create a File object from a URI. | 170 * Create a File object from a URI. |
56 * | 171 * |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
606 sb.write(": $osError"); | 721 sb.write(": $osError"); |
607 if (path != null) { | 722 if (path != null) { |
608 sb.write(", path = '$path'"); | 723 sb.write(", path = '$path'"); |
609 } | 724 } |
610 } else if (path != null) { | 725 } else if (path != null) { |
611 sb.write(": $path"); | 726 sb.write(": $path"); |
612 } | 727 } |
613 return sb.toString(); | 728 return sb.toString(); |
614 } | 729 } |
615 } | 730 } |
OLD | NEW |