Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Monday, July 13, 2015

Input and Output in .NET

Input and Output in .NET
To make a crude generalization, the input/output functions in the .NET Framework can be divided into two broad categories, irrespective of the data storage (disk, memory, etc.) that is being written to or read from.

Data can be treated as a stream of bytes or characters. For example, we could read 500 bytes from a file and write them to a memory buffer. Data can also be treated as a set of objects. Reading and writing the objects is referred to as deserializing and serializing the objects. We can serialize (write) the list of Customer objects to disk. We can then deserialize (read) the list of Customer objects back into memory.

The System::IO namespace has several classes for reading and writing to various types of storage while treating the data as bytes or characters. Serialization functionality can be found in various places in the .NET framework. The System::Runtime::Serialization namespace handles serialization of the Common Type System. The System::Xml::Serialization namespace handles XML serialization.

Stream Classes
Stream is an abstract class that is the basis for reading from and writing bytes to some storage such as a file. It supports both synchronous and asynchronous reading and writing. Asynchronous methods are discussed later in this chapter. The Stream class has the typical methods that you would expect: Read, Write, Seek, Flush, and Close.

The FileStream class is derived from Stream to represent the reading and writing of files as a series of bytes. The FileStream constructor builds the actual stream instance. The overridden Stream methods implement the reading and writing to the file.

Other classes derived from Stream include MemoryStream, BufferedStream, and NetworkStream (in System::Net::Sockets).

The FileStream example (in the FileIO directory with the IO examples) illustrates how to use the Stream classes. If the file does not exist, a new file is created and the numbers from 0 to 9 are written to the file. If the file already exists, the code starts reading 5 bytes from the end of the file and then writes them out. (You should run the example twice. The first time creates and writes the file, and the second time reads and displays the file.)

      unsigned char data __gc[] =
         new unsigned char __gc [10];
      FileStream *fs = new FileStream(
         "FileStreamTest.txt", FileMode::OpenOrCreate);
      if (fs->Length == 0)
      {
         Console::WriteLine("Writing Data...");
         for (short i = 0; i < 10; i++)
            data[i] = (unsigned char)i;
         fs->Write(data, 0, 10);
      }
      else
      {
         fs->Seek(-5, SeekOrigin::End);
         int count = fs->Read(data, 0, 10);
         for (int i = 0; i < count; i++)
         {
            Console::WriteLine(data[i]);
         }
      }
      fs->Close();
Share:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

Unordered List