StreamReader and StreamWriter In Hindi
सी# प्रोग्रामिंग में स्ट्रीमरीडर और स्ट्रीम राइटर सी# में बिल्ट-इन फाइल हैंडलिंग ऑपरेशन क्लास हैं, ये फाइल क्लास System.IO नामस्थान में इनबिल्ट मौजूद हैं, सी# प्रोग्रामर सी# में इन क्लास को मौजूदा फ़ाइलों या स्ट्रीम से डाटा और इनफार्मेशन को रीड और राइट करने में उपयोग करते हैं। स्ट्रीमरीडर और स्ट्रीम राइटर क्लास सी# प्रोग्रामर टेक्स्ट डेटा को फाइल हैंडलिंग में मैनेज रीड और राइट करने में हेल्प करते हैं, जहा सी# प्रोग्रामर मौजूदा टेक्स्ट और अन्य फाइल्स फॉर्मेट में फ़ाइलों से रीड और राइट ऑपरेशन कर सकते हैं।

So, let’s get to know better about StreamReader and StreamWriter built-in class function in file handling in C# programming.
StreamReader File Handling Function in C# Programming.
सी# प्रोग्रामिंग में स्ट्रीमरीडर क्लास का उपयोग सी# प्रोग्रामिंग में बनाई गई मौजूदा फाइल या अन्य यूजर इनपुट स्ट्रीम, जैसे मेमोरी लोकेशन या नेटवर्क स्ट्रीम लोकेशन से टेक्स्ट इनफार्मेशन को रीड करने में किया जाता है। स्ट्रीमरीडर क्लास सी# प्रोग्रामिंग में स्पेशल करैक्टर एन्कोडिंग फॉर्मेट जैसे, UTF-8 या ASCII में टेक्स्ट फाइल करैक्टर फॉर्मेट में रीड करता है, और जरूरत के अनुसार सी# प्रोग्रामिंग में लार्ज टेक्स्ट फ़ाइलों को एफ्फिसेंटली रीड और कस्टमाइज करने में हेल्प करता है।
Creating and using StreamReader in C# programming.
So, let’s read file with StreamReader class in C# programming.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @”C:\path\to\your\sample.txt”;
// let Use StreamReader class to read from a file
try
{
using (StreamReader reader = new StreamReader(filePath))
{
string line;
// it Reading line by line till null
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line); // it produce Output each line in the file
}
}
}
catch (FileNotFoundException e)
{
Console.WriteLine(“\n Display File Read Error: File not found. ” + e.Message);
}
catch (Exception e)
{
Console.WriteLine(“File An error occurred: ” + e.Message);
}
}
}
Main methods of StreamReader file reading.
- ReadLine() – सी# प्रोग्राम में एक्टिव या करंट लोकशन स्ट्रीम से फाइल टेक्स्ट की एक रौ को रीड करता है।
- ReadToEnd() – सी# प्रोग्राम में स्ट्रीम फाइल के करंट लोकेशन से एन्ड तक सभी फाइल करैक्टर को रीड करता है।
- Read() – यह मौजूदा सी# प्रोग्राम में स्ट्रीम से नेक्स्ट करैक्टर को रीड करता है।
- Peek() – यह मौजूदा सी# प्रोग्राम में करंट लोकेशन को नेक्स्ट मूव किए बिना नेक्स्ट करैक्टर रिटर्न करता है, यह एक तरह से LookAhead के समान कार्य करता है।
Reading all content with ReadToEnd in C# program example.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @”C:\path\to\your\sample.txt”;
try
{
using (StreamReader reader = new StreamReader(filePath))
{
// it will Read the entire content of the active file
string content = reader.ReadToEnd();
Console.WriteLine(content); // it will produce Output entire content of the file
}
}
catch (Exception e)
{
Console.WriteLine(“\n It display An error occurred ” + e.Message);
}
}
}
StreamWriter File Handling Function in C# Programming.
सी# प्रोग्रामिंग में स्ट्रीम राइटर क्लास का उपयोग फ़ाइल या अन्य आउटपुट स्ट्रीम या फाइल में टेक्स्ट इनफार्मेशन को राइट करने में किया जाता है। सी# प्रोग्रामिंग में स्ट्रीम राइटर क्लास का मुख्य उपयोग नई टेक्स्ट फ़ाइलों को क्रिएट करने में या मौजूदा फाइल को ओवरराइट करने में किया जाता है, इसके साथ ही स्ट्रीमराइटर क्लास मौजूदा फाइल या फ़ाइलों में टेक्स्ट इनफार्मेशन को अपेण्ड करने में भी कुशल है।
Creating and using the StreamWriter class in C# programming.
So, let’s take an example of using the Stream Writer class in C# programming.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @”C:\path\to\your\sample.txt”;
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
// it will Writing number of below lines to a sample.txt file
writer.WriteLine(“\n Hi, Vcanhelpsu.com”);
writer.WriteLine(“\n Lets Learn About Programming.”);
}
Console.WriteLine(“\n It write in file, when file is new or empty.”);
}
catch (Exception e)
{
Console.WriteLine(“\n file write operation An error occurred ” + e.Message);
}
}
}
Main methods of StreamWriter in C# programming.
- Write(string value) – सी# प्रोग्राम में पर्टिकुलर इंडिकेटेड स्ट्रिंग को स्ट्रीम में राइट करता है।
- WriteLine(string value) – सी# प्रोग्राम में इंडिकेटेड स्ट्रिंग को स्ट्रीम में राइट करता है, बाद में एक नया लाइन करैक्टर क्रिएट कर राइट करता है।
- Flush() – मौजूदा सी# प्रोग्राम में बफ़र मेमोरी को क्लियर करता है, और किसी भी मौजूदा फाइल डाटा बफ़र किए गए डेटा को इनबिल्ट स्ट्रीम में राइट करता है।
- Close() – मौजूदा सी# प्रोग्राम में स्ट्रीम राइटर क्लास को क्लोज करता है, यह फाइल हैंडलिंग फंशन फाइल से जुड़े सभी रिसोर्सेज को क्लोज करता है।
Adding Text to File in C# Program Example.
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @”C:\path\to\your\sample.txt”;
try
{
// below statement Open the file in append mode
using (StreamWriter writer = new StreamWriter(filePath, append: true))
{
writer.WriteLine(“\n it will append text of line in existing file.”);
}
Console.WriteLine(“\n The Text will be appended to the file.”);
}
catch (Exception e)
{
Console.WriteLine(“\n The file text append An error occurred ” + e.Message);
}
}
}
Comparison of StreamReader and StreamWriter c# function in file handling
Feature | StreamReader class function | StreamWriter class function |
Purpose | It Reads text from a file or user stream. | It Writes text to a file or user stream. |
Encoding | It will Reads text using specified or default encoding character format. | It will Writes text using specified or default encoding character format. |
Common Methods | ReadLine(), ReadToEnd(), Read() are common method to read from file. | Write(), WriteLine(), Flush() are common method to write in file. |
Access Type | Streamreader class provide only Read-only access | Streamwriter class function allow permission to Write-only access |
Error Handling | You Can apply throw FileNotFoundException or IOException | You Can use throw UnauthorizedAccessException or IOException |
File Operations with StreamReader and StreamWriter Classes in C# Programming.
Read and Write File with Specific Encoding in C# Program.
आप अपने सी# प्रोग्राम में स्ट्रीमरीडर और स्ट्रीम राइटर क्लास क्रिएट करते समय फाइल करैक्टर एन्कोडिंग को इंडीकेट कर सकते हैं, जिससे से यह तय हो सके कि टेक्स्ट सही एन्कोडिंग फॉर्मेट में रीड या राइट किया गया है।
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
string filePath = @”C:\path\to\your\sample.txt”;
// it will Writing with steamwritee calss in UTF-8 encoding format
using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8))
{
writer.WriteLine(“\n The line of text is written with UTF-8 encoding character format.”);
}
// it will Reading with UTF-8 encoding character format
using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
string content = reader.ReadToEnd();
Console.WriteLine(content);
}
}
}
Handling Exceptions in C# Program.
सी# प्रोग्राम में फ़ाइल हैंडलिंग I/O ऑपरेशन के साथ काम करते समय, हमेशा प्रोग्राम एक्सेप्शन प्रॉपर्ली मैनेज करे, क्योंकि कई बार फ़ाइल एक्सेस करते समय कई कारणों से फेलियर हो सकती है. जैसे, फाइल ऑपरेशन अप्लाई करते समय, फ़ाइल नॉट फाउंड, नो परमिशन टू एक्सेस फाइल, डिस्क इज फुल, आदि कुछ कॉमन फाइल रिलेटेड एक्सेप्शन हैं.
FileNotFoundException
UnauthorizedAccessException
IOException
सी# प्रोग्राम में ऊपर दिए गए प्रोग्राम एक्सेप्शन को मैनेज करने के लिए आप try-catch ब्लॉक का उपयोग कर सकते है, और चेक करें कि फाइल प्रोग्राम अक्सीडेंटली क्रैश न हो जाए।
StreamReader and StreamWriter in C# Programming Conclusion.
सी# प्रोग्राम में स्ट्रीमरीडर फ़ाइलों से टेक्स्ट रीड करने के लिए एक सही क्लास है, और यह आपको सी# प्रोग्राम में डेटा को सीक्वेंस में एक साथ टेक्स्ट रीड करने के लिए ReadLine() और ReadToEnd() जैसी विभिन्न क्लास फंक्शन मेथड को सपोर्ट करता है।
सी# प्रोग्राम में स्ट्रीमराइटर क्लास को फाइल हैंडलिंग में फ़ाइलों में टेक्स्ट राइट करने के लिए डिज़ाइन किया गया है, जिसमें पहले से ही Write() और WriteLine() जैसी क्लास मेथड हैं, जो सी# प्रोग्राम में डेटा को एफ्फिसेंटली राइट परमिशन प्रोवाइड करते हैं।
सी# प्रोग्राम में स्ट्रीमरीडर और स्ट्रीमराइटर क्लास प्रोग्रामर को स्पेशल करैक्टर एनकोडिंग के साथ काम करने और लार्ज फ़ाइलों को एफ्फिसेंटली मैनेज करने में हेल्प करती हैं।