File Handling involves management of files. Files are a collection of data that is stored in a disk with a name and a directory path. Files contain input as well as output streams that are used for reading and writing data respectively.
The System.IO Namespace has various classes and types that allow reading of files and writing to files. Some of the classes in this namespace are given as follows:
Source: MSDN
Classes | Description |
---|---|
FileStream | This class provides a Stream for a file, supporting both synchronous and asynchronous read and write operations. |
StreamReader | This class implements a TextReader that reads characters from a byte stream in a particular encoding. |
StreamWriter | This class implements a TextWriter for writing characters to a stream in a particular encoding. |
TextReader | This class represents a reader that can read a sequential series of characters. |
TextWriter | This class represents a writer that can write a sequential series of characters. This class is abstract. |
BinaryReader | This class reads primitive data types as binary values in a specific encoding. |
BinaryWriter | This class writes primitive types in binary to a stream and supports writing strings in a specific encoding. |
StringReader | This class implements a TextReader that reads from a string. |
StringWriter | This class implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder. |
FileInfo | This class provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited. |
DirectoryInfo | This class exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited. |
The examples using various classes in the System.IO namespace are given as follows:
This class provides a Stream for a files. It supports both synchronous as well as asynchronous read and write operations.
A program that demonstrates the FileStream Class is as follows:
Source Code: Program to implement FileStream Class in C#
using System; using System.IO; namespace FileHandlingDemo { public class Example { public static void Main(string[] args) { FileStream fi = new FileStream("e:\\file.txt", FileMode.OpenOrCreate); fi.WriteByte(12); fi.WriteByte(6); fi.WriteByte(30); fi.Close(); FileStream fo = new FileStream("e:\\file.txt", FileMode.OpenOrCreate); int i = 0; Console.WriteLine("The contents of the file are:"); while ((i = fo.ReadByte()) != -1) { Console.WriteLine(i); } fo.Close(); } } }
The output of the above program is as follows:
The contents of the file are: 12 6 30
The StreamReader Class reads the characters from a byte stream using a TextReader. The Read() and ReadLine() methods are used to read the data from the stream. The StreamWriter Class writes the characters to a byte stream.
A program that demonstrates the StreamReader and StreamWriter Class is as follows:
Source Code: Program to implement StreamReader and StreamWriter Class in C#
using System; using System.IO; namespace FileHandlingDemo { public class Example { public static void Main(string[] args) { FileStream f1 = new FileStream("e:\\file.txt", FileMode.OpenOrCreate); StreamWriter s1 = new StreamWriter(f1); s1.WriteLine("File Handling in C#"); s1.Close(); f1.Close(); FileStream f2 = new FileStream("e:\\file.txt", FileMode.OpenOrCreate); StreamReader s2 = new StreamReader(f2); string data = s2.ReadLine(); Console.WriteLine("The data in the file is as follows:"); Console.WriteLine(data); s2.Close(); f2.Close(); } } }
The output of the above program is as follows:
The data in the file is as follows: File Handling in C#
The TextReader Class has a reader that reads the characters from the file while the TextWriter Class has a writer that writes the characters into the file.
A program that demonstrates the TextReader and TextWriter Class is as follows:
Source Code: Program to implement TextReader and TextWriter Class in C#
using System; using System.IO; namespace FileHandlingDemo { class Example { static void Main(string[] args) { using (TextWriter tw = File.CreateText("e:\\file.txt")) { tw.WriteLine("C# File Handling"); tw.WriteLine("TextReader Class and TextWriter Class"); } using (TextReader tr = File.OpenText("e:\\file.txt")) { Console.WriteLine(tr.ReadToEnd()); } } } }
The output of the above program is as follows:
C# File Handling TextReader Class and TextWriter Class
The BinaryReader Class reads the data types in the form of binary information and the BinaryWriter Class writes the binary information into the stream.
A program that demonstrates the BinaryReader and BinaryWriter Class is as follows:
Source Code: Program to implement BinaryReader and BinaryWriter Class in C#
using System; using System.IO; namespace FileHandlingDemo { class Example { static void Main(string[] args) { string fName = "e:\\file.dat"; using (BinaryWriter bw = new BinaryWriter(File.Open(fName, FileMode.Create))) { bw.Write("File Handling in C#"); bw.Write("BinaryReader class and BinaryWriter class"); } using (BinaryReader br = new BinaryReader(File.Open("e:\\file.dat", FileMode.Open))) { Console.WriteLine(br.ReadString()); Console.WriteLine(br.ReadString()); } } } }
The output of the above program is as follows:
File Handling in C# BinaryReader class and BinaryWriter class
The StringReader Class reads the data from the string using TextReader. Similarly, the StringWriter Class writes the data to the string using the TextWriter.
A program that demonstrates the StringReader and StringWriter Class is as follows:
Source Code: Program to implement StringReader and StringWriter Class in C#
using System; using System.IO; namespace FileHandlingDemo { class Example { static void Main(string[] args) { StringWriter sw = new StringWriter(); sw.WriteLine("File Handling in C#"); sw.WriteLine("StringReader Class and StringWriter Class"); sw.Close(); StringReader sr = new StringReader(sw.ToString()); while (sr.Peek() > -1) { Console.WriteLine(sr.ReadLine()); } } } }
The output of the above program is as follows:
File Handling in C# StringReader Class and StringWriter Class
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *