Wednesday, September 15, 2010

Reading and Writing into files


The FileStream Class:

            The filestream class provides the basic functionality to open file streams for reading and writing. The filestream has 15 overloaded constructors,  u can find them at the following link.

The most commonly used constructor is

FileStream fs = new FileStream("D:\\sample.txt", FileMode.Append, FileAccess.Write);

The filestream class has the following three parameters: 
(a)   string path (b) FileMode  Enumerator (c) FileAcessEnumerator

FileMode Enumerator:

The File Mode enumeration provides members that specify how a file is to be opened or created: -
FileMode.Append
Opens a file and moves the pointer in the FileStream to end of the file. Can be used only with FileAccess.Write.
FileMode.Create
Creates a new file. If the file already exists, it is overwritten.
FileMode.CreateNew
Creates a new file. If the file already exists, an exception is thrown.
FileMode.Open
Opens an existing file. If the file does not exist, an exception is thrown.
FileMode .OpenOrCreate
Opens an existing file. If the file does not exist, it creates a  new file.

FileAccess Enumerator:

The FileAccess enumeration provides members that are used to determine the rights required when opening a file: -

Read
Specifies that the file should be opened with read-only access.
Write
Specifies that the file should be opened to be written to. The file cannot be read, only appended to.
ReadWrite
Specifies full access to the file for reading or writing. Equiv-alent to combining Read and Write values.

StreamReader Class:

The StreamReader class provides the basic functionality to read data from a Stream.
Initializing a StreamReader class:

FileStream fs = new FileStream("D:\\sample.txt", FileMode.Open, FileAccess.Read); 
StreamReader reader = new StreamReader(fs);

Reading Data From a stream:

The following are some of the methods to read data from stream:

  1. reader.Read(): Reads a maximum of count characters from current stream into buffer, beginning at index.
            char[] c = new char[5]; reader.Read(c, 0, 3);
   Console.WriteLine(c);
  1. reader.ReadLine() : Reads a line of characters from the current string and returns as string.
         string d = reader.ReadLine();
          Console.WriteLine(d);
  1. reader.ReadToEnd(): Reads the stream from current position to the end of the stream
         string d = reader.ReadToEnd();
          Console.WriteLine(d);

StreamWriter():

The StreamWriter Class provides basic functionality to write data to text files.

Initializing a streamwriter class:

    FileStream fs = new FileStream("D:\\sample.txt", FileMode.Append, FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs);

Writing text to a stream:

The following are the available methods to write data to stream:
  1. Write(); Is used to write a string value to underlying stream.
             sw.Write("visual studio");

  1. WriteLine() : Is used to write a new line with specified format i.e, string, int, long, boolean values …. etc.,
       sw.WriteLine("THIS IS THE FIRST LINE");
  1. Flush(): is used to clear the buffer and write the text to the underlying stream.
  2. Close(); is used to close the underlying stream.
 Complete code to write text to a file using Stream writer:

FileStream fs = new FileStream("D:\\sample.txt", FileMode.Append,           FileAccess.Write);
      StreamWriter sw = new StreamWriter(fs);
      sw.WriteLine("THIS IS THE FIRST LINE");
      sw.Flush();
      sw.Close();
      fs.Close();

No comments:

Post a Comment