|
- Author:
- kris (IP: 17.228.23.90)
- Timestamp:
- 05/09/09 01:59:29 (15 years ago)
- Comment:
adjusted for FileConduit? changes
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ChapterIoFileSystem
v8 |
v9 |
|
16 | 16 | [[Image(source:/trunk/doc/images/classdia.tango.io.FilePath.png)]] |
---|
17 | 17 | |
---|
18 | | !FilePath is intended to be mutable, thus each transformation modifies the internal content. There is a read-only view of !FilePath (!PathView) that can be used to expose an immutable perspective. The current class-based implementation will likely migrate to a struct version once the D language gains support for struct-constructors. |
---|
| 18 | !FilePath is intended to be mutable, thus each transformation modifies the internal content. There is a read-only view of !FilePath (!PathView) that can be used to expose an immutable perspective. The current class-based implementation will likely migrate to a struct version once the D language gains support for struct-constructors (see module tango.io.Path for more information). |
---|
19 | 19 | |
---|
20 | 20 | A number of common file and directory operations are exposed via !FilePath, including creation, renaming, removal, and folder/directory content listing. A handful of attributes such as file size and various timestamps are also made available. |
---|
78 | 78 | |
---|
79 | 79 | |
---|
80 | | == !FileConduit == |
---|
81 | | |
---|
82 | | The principal means of file access is via a !FileConduit, providing both streaming and random-access to file content. Opening a file for reading is performed as follows: |
---|
83 | | {{{ |
---|
84 | | #!d |
---|
85 | | auto conduit = new FileConduit ("myFilePath"); |
---|
| 80 | == File == |
---|
| 81 | |
---|
| 82 | The principal means of file access is via a File, providing both streaming and random-access to file content. Opening a file for reading is performed as follows: |
---|
| 83 | {{{ |
---|
| 84 | #!d |
---|
| 85 | auto conduit = new File ("myFilePath"); |
---|
86 | 86 | }}} |
---|
87 | 87 | |
---|
89 | 89 | {{{ |
---|
90 | 90 | #!d |
---|
91 | | auto conduit = new FileConduit ("myFilePath", FileConduit.WriteCreate); |
---|
| 91 | auto conduit = new File ("myFilePath", File.WriteCreate); |
---|
92 | 92 | }}} |
---|
93 | 93 | |
---|
94 | 94 | There are a variety of pre-defined styles, including appending, read-only, read-write, create-always and so on. Additional styles can be defined, using a combination of a dozen system-level flags. |
---|
95 | 95 | |
---|
96 | | !FileConduit enables direct, type-agnostic access to file content. In this example we copy a file directly to the console: |
---|
| 96 | File enables direct, type-agnostic access to file content. In this example we copy a file directly to the console: |
---|
97 | 97 | {{{ |
---|
98 | 98 | #!d |
---|
99 | 99 | // open a file for reading |
---|
100 | | auto from = new FileConduit ("test.txt"); |
---|
| 100 | auto from = new File ("test.txt"); |
---|
101 | 101 | |
---|
102 | 102 | // display file content on console |
---|
108 | 108 | #!d |
---|
109 | 109 | // open a file for reading |
---|
110 | | auto from = new FileConduit ("test.txt"); |
---|
| 110 | auto from = new File ("test.txt"); |
---|
111 | 111 | |
---|
112 | 112 | // open another for writing |
---|
113 | | auto to = new FileConduit ("copy.txt", FileConduit.WriteCreate); |
---|
| 113 | auto to = new File ("copy.txt", File.WriteCreate); |
---|
114 | 114 | |
---|
115 | 115 | // copy file |
---|
121 | 121 | #!d |
---|
122 | 122 | // open file for reading |
---|
123 | | auto file = new FileConduit ("test.txt"); |
---|
| 123 | auto file = new File ("test.txt"); |
---|
124 | 124 | |
---|
125 | 125 | // create an array to house the entire file |
---|
130 | 130 | }}} |
---|
131 | 131 | |
---|
132 | | Conversely, one may write directly to a !FileConduit, like so: |
---|
| 132 | Conversely, one may write directly to a File, like so: |
---|
133 | 133 | {{{ |
---|
134 | 134 | #!d |
---|
135 | 135 | // open file for writing |
---|
136 | | auto to = new FileConduit ("text.txt", FileConduit.WriteCreate); |
---|
| 136 | auto to = new File ("text.txt", File.WriteCreate); |
---|
137 | 137 | |
---|
138 | 138 | // write an array of content to it |
---|
141 | 141 | |
---|
142 | 142 | |
---|
143 | | !FileConduit supports random IO also. In this example we relocate the current file position using seek() and, to add a little spice, utilize a !DataStream (reader & writer pair) to perform simple typed input and output: |
---|
| 143 | File supports random IO also. In this example we relocate the current file position using seek() and, to add a little spice, utilize a Data stream (reader & writer pair) to perform simple typed input and output: |
---|
144 | 144 | {{{ |
---|
145 | 145 | #!d |
---|
146 | 146 | // open a file for reading and writing |
---|
147 | | auto file = new FileConduit ("random.bin", FileConduit.ReadWriteCreate); |
---|
148 | | |
---|
149 | | // bind a DataStream to this conduit |
---|
| 147 | auto file = new File ("random.bin", File.ReadWriteCreate); |
---|
| 148 | |
---|
| 149 | // bind a Data stream to this conduit |
---|
150 | 150 | auto input = new DataInput (file); |
---|
151 | 151 | auto output = new DataOutput (file); |
---|
154 | 154 | char[] y = "hello"; |
---|
155 | 155 | |
---|
156 | | // write data, and flush output since DataStream IO is buffered |
---|
157 | | output.putInt (x); |
---|
158 | | output.put (y); |
---|
159 | | output.flush (); |
---|
| 156 | // write data, and flush output since Data IO is buffered |
---|
| 157 | output.int32 (x); |
---|
| 158 | output.array (y); |
---|
| 159 | output.flush; |
---|
160 | 160 | |
---|
161 | 161 | // rewind to file start |
---|
163 | 163 | |
---|
164 | 164 | // read data back again |
---|
165 | | x = input.getInt; |
---|
166 | | input.get (y); |
---|
167 | | }}} |
---|
168 | | |
---|
169 | | Note that in the above example we are using buffered IO, via the !DataStream, and thus need to flush the output before relocating the current position. |
---|
170 | | |
---|
171 | | Each !FileConduit should be explicitly closed when no longer needed. It can often be convenient to use a scope expression for this purpose: |
---|
172 | | {{{ |
---|
173 | | #!d |
---|
174 | | auto file = new FileConduit ("myFilePath"); |
---|
175 | | scope (exit) |
---|
176 | | file.close; |
---|
| 165 | x = input.int32; |
---|
| 166 | y = input.array; |
---|
| 167 | }}} |
---|
| 168 | |
---|
| 169 | Note that in the above example we are using buffered IO, via the Data stream, and thus need to flush the output before relocating the current position. |
---|
| 170 | |
---|
| 171 | Each File should be explicitly closed when no longer needed. It can often be convenient to use a scope expression for this purpose: |
---|
| 172 | {{{ |
---|
| 173 | #!d |
---|
| 174 | scope file = new File ("myFilePath"); |
---|
| 175 | }}} |
---|
| 176 | |
---|
| 177 | As a convenience, File has static functions to read, write or append. For example, to read all file content: |
---|
| 178 | {{{ |
---|
| 179 | #!d |
---|
| 180 | auto content = File.get ("myfile"); |
---|
| 181 | }}} |
---|
| 182 | |
---|
| 183 | The underlying file is closed before the call returns. File must avoid making assumptions about the file content, so the above example returns an array of void. When working with text files, it is necessary to cast the return value to represent the correct data type. For text files this is often a char[]: |
---|
| 184 | {{{ |
---|
| 185 | #!d |
---|
| 186 | auto content = cast(char[]) File.get ("myfile"); |
---|
| 187 | }}} |
---|
| 188 | |
---|
| 189 | To convert a text file into a set of lines, try the following: |
---|
| 190 | {{{ |
---|
| 191 | #!d |
---|
| 192 | import Text = tango.text.Util; |
---|
| 193 | |
---|
| 194 | auto content = cast(char[]) File.get ("myfile"); |
---|
| 195 | auto lines = Text.splitLines (content); |
---|
| 196 | }}} |
---|
| 197 | |
---|
| 198 | Using a foreach to iterate instead: |
---|
| 199 | {{{ |
---|
| 200 | #!d |
---|
| 201 | foreach (line; Text.lines (cast(char[]) File.get("myfile")) |
---|
| 202 | // do something with each line |
---|
| 203 | }}} |
---|
| 204 | |
---|
| 205 | Files can be set to the content of an array: |
---|
| 206 | {{{ |
---|
| 207 | #!d |
---|
| 208 | char[] myText; |
---|
| 209 | |
---|
| 210 | File.set ("myfile", myText); |
---|
| 211 | }}} |
---|
| 212 | |
---|
| 213 | File content may be appended in a similar fashion: |
---|
| 214 | {{{ |
---|
| 215 | #!d |
---|
| 216 | char[] myText; |
---|
| 217 | |
---|
| 218 | File.append ("myfile", myText); |
---|
177 | 219 | }}} |
---|
178 | 220 | |
---|
179 | 221 | [[Image(source:/trunk/doc/images/classdia.tango.io.FileConduit.png)]] |
---|
180 | 222 | |
---|
181 | | === !FileConduit Exceptions === |
---|
182 | | |
---|
183 | | IO exceptions are raised where a bulk read or write operation fails entirely, or where a copy operation fails to complete. This might happen if, for example, a remote file were to suddenly become unavailable whilst in use. |
---|
184 | | |
---|
185 | | |
---|
186 | | == File == |
---|
187 | | |
---|
188 | | File is a simple class for read, write or append file content at once. |
---|
189 | | |
---|
190 | | [[Image(source:/trunk/doc/images/classdia.tango.io.File.png)]] |
---|
191 | | |
---|
192 | | For example, to read all file content: |
---|
193 | | {{{ |
---|
194 | | #!d |
---|
195 | | auto file = new File ("myfile"); |
---|
196 | | auto content = file.read(); |
---|
197 | | }}} |
---|
198 | | |
---|
199 | | The underlying file is closed before the call returns. File must avoid making assumptions about the file content, so the above example returns an array of void. When working with text files, it is necessary to cast the return value to represent the correct data type. For text files this is often a char[]: |
---|
200 | | {{{ |
---|
201 | | #!d |
---|
202 | | auto file = new File ("myfile"); |
---|
203 | | auto content = cast(char[]) file.read(); |
---|
204 | | }}} |
---|
205 | | |
---|
206 | | To convert a text file into a set of lines, try the following: |
---|
207 | | {{{ |
---|
208 | | #!d |
---|
209 | | import Text = tango.text.Util; |
---|
210 | | |
---|
211 | | auto file = new File ("myfile"); |
---|
212 | | auto content = cast(char[]) file.read; |
---|
213 | | auto lines = Text.splitLines (content); |
---|
214 | | }}} |
---|
215 | | |
---|
216 | | Using a foreach to iterate instead: |
---|
217 | | {{{ |
---|
218 | | #!d |
---|
219 | | foreach (line; Text.lines (content)) |
---|
220 | | // do something with each line |
---|
221 | | }}} |
---|
222 | | |
---|
223 | | Files can be set to the content of an array: |
---|
224 | | {{{ |
---|
225 | | #!d |
---|
226 | | char[] myText; |
---|
227 | | |
---|
228 | | file.write (myText); |
---|
229 | | }}} |
---|
230 | | |
---|
231 | | File content may be appended in a similar fashion: |
---|
232 | | {{{ |
---|
233 | | #!d |
---|
234 | | char[] myText; |
---|
235 | | |
---|
236 | | file.append (myText); |
---|
237 | | }}} |
---|
238 | | |
---|
239 | | Each of the methods belonging to !FilePath are exposed via the ''path'' method, so you can get the file size, relocate it, remove it, and so on. |
---|
240 | | |
---|
241 | 223 | === File Exceptions === |
---|
242 | 224 | |
---|
243 | | File will throw IO exceptions where an underlying OS or file-system error occurs. This might happen when, for example, an attempt is made to write a read-only file. |
---|
244 | | |
---|
| 225 | IO exceptions are raised where a bulk read or write operation fails entirely, or where a copy operation fails to complete. This might happen when, for example, attempting to write a read-only file. |
---|
245 | 226 | |
---|
246 | 227 | == !FileSystem == |
|
|