Input and Output: System.IO

< BACK  NEXT >
[oR]

Like most software, .NET Framework applications need some way to input and output data. These operations are most commonly done against a disk drive, but there are other possibilities too. The .NET Framework class library's System.IO namespace contains a substantial set of types that developers can use to read and write files, work with directories in a file system, and do other kinds of straightforward data access.

File access is an essential service

Among the most fundamental of these types is the Stream class. As defined by this class, a stream is a sequence of bytes together with methods to read the stream's contents, write those contents, perhaps seek to a specific location in the stream, and perform other operations. Specific stream classes inherit from a common abstract Stream class and so provide access to various kinds of information in a consistent way.

A Stream object contains a sequence of bytes and provides methods to access those bytes

For example, information stored in files can be accessed using the File class. While an instance of File provides familiar methods for working with files such as Create, Delete, and Open, it doesn't provide methods for reading and writing a file's contents. Instead, a File object's Create and Open methods return an instance of a FileStream that can be used to get at the file's contents. Like all streams, a FileStream object provides Read and Write methods for synchronous access to a file's data, that is, for calls that block waiting for data to be read or written. Also like other streams, FileStream objects allow asynchronous access using the paired BeginRead/EndRead and BeginWrite/EndWrite methods. These methods allow a .NET Framework application to begin a read or write operation and then check later to get the result. Each FileStream also provides a Seek method to move to a designated point in the file, a Flush method to write data to the underlying device (such as a disk drive), a Close method to close the FileStream, and many more methods.

A FileStream object allows access to a file's contents as binary data

FileStreams work only with binary data, however, which isn't always what's needed. System.IO provides other standard classes to work with file data in other formats. For example, the FileInfo class can be used to create FileStreams, but it can also be used to create instances of the classes StreamReader and StreamWriter. Unlike File, whose methods are mostly static, an instance of a FileInfo class must be explicitly created before its methods can be used. Once a FileInfo object exists, its OpenText method can be used to create a new StreamReader object. This StreamReader can then be used to read characters from whatever file is associated with the FileInfo object.

A FileInfo object allows access to a file's contents as text

Here's a C# example that illustrates how these classes can be used:

 using System; using System.IO; class FileIOExample {     static void Main()     {        FileStream fs;        FileInfo f;        StreamReader sr;        byte[] buf = new byte[10];        string s;        int i;        for (i=0; i<10; i++)              buf[i] = (byte) (65 + i);        fs = File.Create("test.dat");        fs.Write(buf, 0, 10);        fs.Close();        f = new FileInfo("test.dat");        sr = f.OpenText();        s = sr.ReadToEnd();        Console.WriteLine("{ 0} ", s);     } } 

This admittedly unrealistic example begins with appropriate using statements and then defines the single class FileIOExample. This class contains only a Main method, which begins with several declarations. After this, the 10-byte buffer buf is populated with the characters A through J. Because buf can accept only bytes, this is done by explicitly calculating each character's value and then forcing the result to be of type byte. (This forced type conversion is called casting.) File's Create method is then used to create a file, followed by a call to File's Write method. This method writes buf's ten characters into that file and is followed by a Close call that closes the file. Because the File class declares all of these methods to be static, they can be invoked without explicitly creating a File instance.

The example next opens the same file using an instance of the FileInfo class. Calling the FileInfo object's OpenText method returns a StreamReader object whose ReadToEnd method can be used to read the characters just written into a string. StreamReaders also provide methods to read single characters, blocks of characters, and lines of characters. Finally, the characters read from the file are written to the console, yielding the result

 ABCDEFGHIJ 

System.IO also defines several other useful types. The Directory class, for instance, provides methods such as CreateDirectory to create a new directory, Delete to destroy an existing directory and its contents, and several more. The MemoryStream class allows the typical operations defined for a stream, such as Read, Write, and Seek, to be carried out on an arbitrary set of bytes in memory. StringWriter and StringReader provide analogous functions to StreamWriter and StreamReader, except that instead of working with files, they work with in-memory strings. BinaryReader and BinaryWriter allow reading and writing values of types such as integers, decimals, and characters from a stream.

Many other classes are also defined for working with files, directories, and streams

While information stored in relational databases is more important for many applications, data stored in files still matters. The classes in System.IO provide a flexible set of options for working with that data.

< BACK  NEXT >


Understanding. NET. A Tutorial and Analysis
Understanding .NET: A Tutorial and Analysis (Independent Technology Guides)
ISBN: 0201741628
EAN: 2147483647
Year: 2002
Pages: 60

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net