![]() Mode: A member of the FileMode enumeration (Append, Create, CreateNew, Open, New FileStream (ByVal path as String, ByVal mode as FileMode, _ByValaccess as FileAccess) If the FileStream object owns the file's handle, a callto the Close method will also close the file's handle and thus decrementits handle count by one. Note: Use this constructor when you have a valid file pointer, need tospecify the read/write permissions, and wish to own (or pass off) thefile's handle. OwnsHandle: Indicates if the file's handle will be owned by the giveninstance of the ![]() New FileStream (ByVal handle as IntPtr, ByVal access as FileAccess, _ByValownsHandle as Boolean) Note: Use this constructor when you know the file's path and wish tospecify how the file is opened. Mode: A member of the FileMode enumeration (Append, Create, CreateNew, Open,OpenOrCreate, Truncate) that specifies how the file should be opened. Path: A valid path to the file that the FileStream object will represent. New FileStream (ByVal path as String, ByVal mode as FileMode) Note: Use this constructor when you have a valid file pointer and need tospecify the read/write permissions. New FileStream (ByVal handle as IntPtr, ByVal access asFileAccess)Īccess: A member of the FileAccess enumeration (Read, ReadWrite, Write). Table 7.12lists the version of the FileStream constructors that can be used to create aFileStream object. You've already seen thatFileInfo uses this class to expose reading and writing to files. The FileStream class can be created explicitly. They arebased on the TextWriter and TextReader classes. As you might haveguessed, these objects are designed to work with persisted text files. These classes expose thenecessary functionality to read and write to files in. You can see that the FileInfo class makes extensive use of theFileStream, StreamWriter, and StreamReader classes. The OpenWrite method creates a FileStream object that is both read andwrite. The OpenRead method creates a FileStream object that is read only. The OpenText method creates a StreamReader object based on the associatedtext file. Themethod has three constructors that allow us to specify the open mode (open,create, append, and so on), the file access (read, write, read and write), andhow we want the file to be shared by other FileStream objects. The Open method opens a file and returns it to us as a FileStream object. The CreateText method creates an instance of the StreamWriter class thatcreates a new text file to which to write. The StreamWriter class implements aTextWriter instance to output the characters in a specific encoding. The AppendText method creates an instance of the StreamWriter class thatallows us to append text to a file. Table 7.11 lists these methodsand their associated return types. You can then call these methods toreturn and write to the contents of this file. ![]() However, they are useful as you often already have an instance ofFileInfo that is specific to a given file. Thesemethods simply use the FileStream and related classes to expose thisfunctionality. ![]() The FileInfo class exposes a number ofmethods that allow us access to stream-related functions based on a file. To read data to and from files using the System.IO namespace, we primarilyuse two classes: FileInfo and FileStream. Thisclass gives us access to files stored on disk inside of directories. An example of a base stream is the FileStream class. These streams that connect directly to backing stores are calledbase streams in. This allows each stream type to read and write bytes to and from its ownbacking store. Backing storerefers to a storage medium, such as a disk, tape, memory, network, and so on.Every backing store implements its own stream type as a version of the Streamclass. Their differences lie in their backing store. This section will define the AppendText, CreateText, Open, OpenRead, andOpenWrite methods of the FileInfo class.įile and data streams are essentially the same thing. NETFramework gives us a group of classes and methods inside the System.IO namespacethat allows us to access data streams and files both synchronously andasynchronously. Ifyou've communicated with disparate systems, you are undoubtedly familiarwith writing out CSV or XML files as a means of exchanging data. Learn More Buy Reading and Writing to Files and StreamsĪs programmers, we often have to write directly to a file or data stream.
0 Comments
Leave a Reply. |