A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.
The stream is basically the sequence of bytes passing through the communication path.
Reading from and Writing into Text files
The StreamReaderand StreamWriterclasses are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.
The StreamReader Class
The StreamReaderclass also inherits from the abstract base class TextReader that represents a reader for reading series of characters.
The StreamWriter Class
The StreamWriterclass inherits from the abstract class TextWriter that represents a writer, which can write a series of character.
Example-1: Stream Reader
Result:
Example-2: Stream Writer
In the above example you can add a text into the existing file. Here Dispose() method is used to release the file. Because without it when you want to open the same file then it will tell you that it is in use now, it means the file is not free now.
Example-3:
In the above example you can write a text into the existing file but the previous data will be deleted.
Example-4:
In the above example text will be written line by line into the file.
Example-5: Binary Reader
Result: only first one character will be displayed.
Example-6:
Result:
Example-7: Binary Writer
Result: alphabet ‘a’ will be copied onto the file’s first character.
Переглядів: 209
Не знайшли потрібну інформацію? Скористайтесь пошуком google: