Stream class
Before going ahead to read from or write to a file, you must know about Stream class. The Stream class is an abstract class which provides a number of virtual or abstract properties and methods for file or memory handling. Some important properties of Stream class are as follows:
public abstract long Length { get; }
public abstract bool CanWrite { get; }
public virtual bool CanTimeout { get; }
public abstract bool CanSeek { get; }
public abstract bool CanRead { get; }
public virtual int ReadTimeout { get; set; }
public virtual int WriteTimeout { get; set; }
Stream Length is used to determine the file size and Position is used to know the current position of cursor during file reading or writing. As long as stream position is less than stream length, loop can continue to read/write file.
FileStream
FileStream class derives from Stream class which provides different constructors to create a FileStream object in different file modes(e.g. create, open, append etc.) and in different file access modes(e.g. read, write, or both). FileAttributes enum is used to define nature of file e.g. System, Hidden, ReadOnly, Temporary or Encrypted file. FileAttributes , FileMode and FileAccess enums belong to System.IO for this.
FileStream is a special form of Stream. Other types of streams such as MemoryStream, NetworkStream etc also derive from Stream class.
File & FileInfo
File, FileInfo and FileStream classes are generally used to create stream for a file. To read or write a file, the file should be open as stream.
File is a static class which provides several static methods to read/write a file in different ways. Note that no property is available in File class. Basically, File is a helper class to read/write/append different files.
In case of FileStream, you first create an instance of FileStream using its any constructor. Then, FileStream object provides methods/properties to read/write a file.
Tip1. If you FileStream object then use ReadByte() to read a byte and automatically advance to the next byte of file stream.
FileStream Example. Read a text file test.txt from MyDocument folder.
using System.Text;
class FileReadTechniques
{
static void Main()
{
var mydocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filepath = Path.Combine(mydocs, "test.txt");
FileStream fileStream = File.Open(filepath, FileMode.Open, FileAccess.Read);
StringBuilder sb = new StringBuilder();
while (fileStream.Position < fileStream.Length)
{
int c = fileStream.ReadByte();
sb.Append(Convert.ToChar(c).ToString());
}
Console.WriteLine(sb.ToString());
fileStream.Close();
}
}
BinaryReader Example. Read a text file test.txt from MyDocument folder.
Note: BinaryReader is good for reading binary files such as image files.
using System.Text;
class Demo
{
static void Main()
{
var mydocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filepath = Path.Combine(mydocs, "test.txt");
using BinaryReader binaryReader =
new BinaryReader(File.Open(filepath, FileMode.Open));
Stream stream = binaryReader.BaseStream;
StringBuilder sb = new StringBuilder();
while (stream.Position < stream.Length)
{
sb.Append(binaryReader.ReadChar());
}
string result = sb.ToString();
Console.WriteLine(result);
}
}
To read/write any stream, you can create instance of StreamReader/StreamWriter class.
StreamReader Example. Read a text file test.txt from MyDocument folder.
Note: StreamReader is good for reading text files.
using System.Text;
class Demo
{
static void Main()
{
var mydocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filepath = Path.Combine(mydocs, "test.txt");
using StreamReader streamReader =
new StreamReader(filepath);
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = streamReader.Read()) != -1)
{
sb.Append((char)ch);
}
string result = sb.ToString();
Console.WriteLine(result);
}
}
Info: File class contains methods to create an object of FileStream, StreamReader or StreamWriter class.
Tip1: These methods name begin with Create/Open.
Tip2. The name of methods of File class begins with Read, Write, Append for read, write and append operations respectively.
Tip3. Use the using statement when creating FileStream, StreamReader or BinaryReader to avoid calling Close or Dispose method at the end of code.
Remember. File.OpenText return type is StreamReader. File.CreateText and File.AppendText return type is StreamWriter.
No comments:
Post a Comment