Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changes between Version 8 and Version 9 of ChapterIoFileSystem

Show
Ignore:
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  
    1616[[Image(source:/trunk/doc/images/classdia.tango.io.FilePath.png)]]  
    1717 
    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)
    1919 
    2020A 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. 
    7878 
    7979 
    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 
     82The 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 
     85auto conduit = new File ("myFilePath"); 
    8686}}} 
    8787 
    8989{{{ 
    9090#!d 
    91 auto conduit = new FileConduit ("myFilePath", FileConduit.WriteCreate); 
     91auto conduit = new File ("myFilePath", File.WriteCreate); 
    9292}}} 
    9393 
    9494There 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. 
    9595 
    96 !FileConduit enables direct, type-agnostic access to file content. In this example we copy a file directly to the console: 
     96File enables direct, type-agnostic access to file content. In this example we copy a file directly to the console: 
    9797{{{ 
    9898#!d 
    9999// open a file for reading 
    100 auto from = new FileConduit ("test.txt"); 
     100auto from = new File ("test.txt"); 
    101101 
    102102// display file content on console 
    108108#!d 
    109109// open a file for reading 
    110 auto from = new FileConduit ("test.txt"); 
     110auto from = new File ("test.txt"); 
    111111 
    112112// open another for writing 
    113 auto to = new FileConduit ("copy.txt", FileConduit.WriteCreate); 
     113auto to = new File ("copy.txt", File.WriteCreate); 
    114114 
    115115// copy file 
    121121#!d 
    122122// open file for reading  
    123 auto file = new FileConduit ("test.txt"); 
     123auto file = new File ("test.txt"); 
    124124 
    125125// create an array to house the entire file 
    130130}}} 
    131131 
    132 Conversely, one may write directly to a !FileConduit, like so: 
     132Conversely, one may write directly to a File, like so: 
    133133{{{ 
    134134#!d 
    135135// open file for writing  
    136 auto to = new FileConduit ("text.txt", FileConduit.WriteCreate); 
     136auto to = new File ("text.txt", File.WriteCreate); 
    137137 
    138138// write an array of content to it  
    141141 
    142142 
    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: 
     143File 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: 
    144144{{{ 
    145145#!d 
    146146// open a file for reading and writing 
    147 auto file = new FileConduit ("random.bin", FileConduit.ReadWriteCreate); 
    148  
    149 // bind a DataStream to this conduit  
     147auto file = new File ("random.bin", File.ReadWriteCreate); 
     148 
     149// bind a Data stream to this conduit  
    150150auto input  = new DataInput  (file); 
    151151auto output = new DataOutput (file); 
    154154char[] y = "hello"; 
    155155 
    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  
     157output.int32 (x); 
     158output.array (y); 
     159output.flush
    160160 
    161161// rewind to file start 
    163163 
    164164// 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; 
     165x = input.int32; 
     166y = input.array; 
     167}}} 
     168 
     169Note 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 
     171Each 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 
     174scope file = new File ("myFilePath"); 
     175}}} 
     176 
     177As a convenience, File has static functions to read, write or append. For example, to read all file content: 
     178{{{ 
     179#!d 
     180auto content = File.get ("myfile"); 
     181}}} 
     182 
     183The 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 
     186auto content = cast(char[]) File.get ("myfile"); 
     187}}} 
     188 
     189To convert a text file into a set of lines, try the following: 
     190{{{ 
     191#!d 
     192import Text = tango.text.Util; 
     193 
     194auto content = cast(char[]) File.get ("myfile"); 
     195auto lines = Text.splitLines (content); 
     196}}} 
     197 
     198Using a foreach to iterate instead: 
     199{{{ 
     200#!d 
     201foreach (line; Text.lines (cast(char[]) File.get("myfile")) 
     202         // do something with each line 
     203}}} 
     204 
     205Files can be set to the content of an array: 
     206{{{ 
     207#!d 
     208char[] myText; 
     209 
     210File.set ("myfile", myText); 
     211}}} 
     212 
     213File content may be appended in a similar fashion: 
     214{{{ 
     215#!d 
     216char[] myText; 
     217 
     218File.append ("myfile", myText); 
    177219}}} 
    178220 
    179221[[Image(source:/trunk/doc/images/classdia.tango.io.FileConduit.png)]] 
    180222 
    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  
    241223=== File Exceptions === 
    242224 
    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  
     225IO 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. 
    245226 
    246227== !FileSystem ==