FileStream Class c# In Hindi

FileStream Class c# In Hindi

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

FileStream Class c# In Hindi

C# प्रोग्राम में बनी फाइल को FileStream क्लास इसके यूजर को बॉटम लेवल पर डिटेल्ड फाइल्स ऑपरेशन परफॉर्म करने में हेल्प करते है. जिससे C# यूजर को फ़ाइल को रीड राइट एक्सेस, परमिशन और लार्ज वॉल्यूम फ़ाइलों को प्रॉपर मैनेज करने पर ज़्यादा कंट्रोल मिलता है।

So, let’s take a closer look at the FileStream class in the C# programming language and all its functional features.

FileStream Overview in C#.

C# प्रोग्राम में FileStream क्लास का यूज़ सामान्य रूप से तब किया जाता है, जब C# यूजर को किसी क्रिएटेड मौजूदा फ़ाइल को बाइट-बाय-बाइट आर्डर में रीड या उसमें डाटा और इनफार्मेशन को राइट करना होता है।

C# प्रोग्राम में FileStream क्लास बाइट लेवल पर ऑपरेट करके StreamReader और StreamWriter से ज़्यादा फीचर्स और फ़्लेक्सिबिलिटी प्रोवाइड करता है. इसका मतलब है कि C# में FileStream क्लास बाइनरी फ़ाइलों जैसे, इमेज, ऑडियो, आदि के साथ-साथ टेक्स्ट फ़ाइलों को भी हैंडल और मैनेज कर सकता है।

C# यूजर FileStream क्लास का यूज़ सिंक्रोनस और एसिंक्रोनस दोनों तरह के फ़ाइल हैंडलिंग ऑपरेशन को अप्लाई करने में कर सकते हैं।

Creating a FileStream Object.

C# यूजर प्रोग्राम में डिजायर फ़ाइल लोकेशन पाथ, डिजायर फाइल एक्सेस टाइप और फाइल ओपन ऑपरेट मोड जैसे, रीड, राइट, और फ़ाइल मोड और फ़ाइल डाटा इनफार्मेशन शेयर जैसे दूसरे ऑप्शन इंडीकेट कर FileStream ऑब्जेक्ट क्लास क्रिएट कर सकते हैं।

Creating a FileStream Object Example.

using System;

using System.IO;

class FilestreamObject

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\sample.dat”;

        try

        {

            // here we Create a FileStream class to open the file for reading and writing purpose only

            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))

            {

                Console.WriteLine(“FileStream claas object created successfully.”);

            }

        }

        catch (IOException excption)

        {

            Console.WriteLine(“Display file opening error. ” + excption.Message);

        }

    }

}

Special parameters for the FileStream class constructor object.

  • filePath – यहाँ C# यूजर उस फ़ाइल का पाथ इंडीकेट करते है, जिसे आप फाइल हैंडलिंग ऑपरेशन के लिए ओपन या क्रिएट करना चाहते हैं।
  • mode – यह मौजूदा फाइल में इंडीकेट करता है कि आपके ऑपरेटिंग सिस्टम में डिजायर फ़ाइल को कैसे ओपन करना चाहिए। यहाँ उस फाइल का FileMode डिफाइन होता है, जिस मोड में उसे ओपन किया जाना हैं.
  • FileMode.Create – यह फाइल हैंडलिंग प्रोसेस में एक नई फ़ाइल क्रिएट करता है। यदि यहाँ फ़ाइल पहले से मौजूद है. तो यह कमांड उसे ओवरराइट कर देता है।
  • FileMode.Open – यह आपके ऑपरेटिंग सिस्टम में स्टोर एक मौजूदा फ़ाइल को ओपन करता है।
  • FileMode.OpenOrCreate – यदि आपके कंप्यूटर सिस्टम में फ़ाइल पहले से मौजूद है, तो यह स्टेटमेंट उसे ओपन करता है, नहीं तो, यह आपके लिए एक नई फ़ाइल क्रिएट करता है।
  • FileMode.Append – यदि आपके कंप्यूटर सिस्टम में फ़ाइल मौजूद है, तो यह स्टेटमेंट उसे ओपन करता है, और मौजूदा फ़ाइल के बॉटम तक जाता है। यदि आपके कंप्यूटर सिस्टम में फ़ाइल मौजूद नहीं है, तो यह एक नई फ़ाइल क्रिएट करता है।
  • access – यह आपके कंप्यूटर सिस्टम में फ़ाइल तक एक्सेस का एक्सेस टाइप इंडीकेट करता है. जैसे, रीड, राइट, या दोनों मेथड हो सकते है।

Here are some common features of FileAccess for file handling in the system.

  • FileAccess.Read – यहाँ आप फाइल हैंडलिंग प्रोसेस में किसी फ़ाइल को रीड करने के लिए ओपन करे।
  • FileAccess.Write – यहाँ आप फाइल हैंडलिंग प्रोसेस में किसी फ़ाइल में डाटा और इनफार्मेशन को राइट करने के लिए ओपन करे।
  • FileAccess.ReadWrite – यहाँ आप फाइल हैंडलिंग प्रोसेस में किसी फ़ाइल को रीड और राइट डाटा दोनों ऑपरेशन के लिए ओपन करे।
  • share – यह फाइल हैंडलिंग प्रोसेस में किसी फ़ाइल को अन्य को शेयरिंग करने का फीचर्स प्रोवाइड करता है। जैसे,
  • FileShare.Read – यहाँ आप फाइल हैंडलिंग प्रोसेस में किसी फ़ाइल ओपन होने पर दूसरे प्रोसेस को फ़ाइल रीड करने में हेल्प करता है।
  • FileShare.None – इस प्रोसेस में आपके सिस्टम में फ़ाइल ओपन होने पर कोई दूसरा प्रोसेस उसे एक्सेस नहीं कर सकता है।

Reading data using a FileStream in C#.

C# यूजर Read() और ReadByte() जैसे फाइल हैंडलिंग फंक्शन या मेथड से FileStream क्लास का यूज़ करके मौजूदा फ़ाइल से बाइट्स डाटा इनफार्मेशन को रीड कर सकते हैं।

Example of reading byte-by-byte data and information from a file.

using System;

using System.IO;

using System.Text;

class Program

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\sample.dat”;

        try

        {

            // here it Check if file exists or not

            if (!File.Exists(filePath))

            {

                Console.WriteLine(“Search File is not found.”);

                return;

            }

            // here it Open and read the file

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))

            using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))

            {

                string content = reader.ReadToEnd();

                Console.WriteLine(content);

            }

        }

        catch (UnauthorizedAccessException)

        {

            Console.WriteLine(“File Access denied.”);

        }

        catch (IOException excption)

        {

            Console.WriteLine(“I/O File Error – ” + excption.Message);

        }

        catch (Exception excption)

        {

            Console.WriteLine(“Unexpected File Error – ” + excption.Message);

        }

    }

}

Example of reading data into a byte array.

यदि C# यूजर प्रोग्राम में एक साथ कई बाइट फाइल रीड चाहते हैं, तो आप बाइट ऐरे में डेटा को रीड करने के लिए Read() फंक्शन मेथड का यूज़ कर सकते हैं।

using System;

using System.IO;

using System.Text;

class Program

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\sample.dat”;

        try

        {

            // here it Check if the file exists or not

            if (!File.Exists(filePath))

            {

                Console.WriteLine(“Search File is not found.”);

                return;

            }

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))

            {

                byte[] buffer = new byte[1024];

                int bytesRead;

                // here we Read bytes into the buffer location

                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)

                {

                    Console.WriteLine($”Read {bytesRead} bytes.”);

                    // here it Convert bytes to text and display info

                    string text = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                    Console.WriteLine(text);

                }

            }

        }

        catch (UnauthorizedAccessException)

        {

            Console.WriteLine(“File Access denied.”);

        }

        catch (IOException excption)

        {

            Console.WriteLine(“File I/O Error occured. ” + excption.Message);

        }

        catch (Exception excption)

        {

            Console.WriteLine(“Unexpected file Error. ” + excption.Message);

        }

    }

}

Writing data to a file using the FileStream class.

C# यूजर फाइल हैंडलिंग प्रोग्राम में FileStream क्लास का यूज़ करके Write() और WriteByte() फंक्शन जैसे मेथड से फ़ाइल में डेटा क्रिएट या राइट कर ​​सकते हैं।

Example of writing data to a file in bytes.

using System;

using System.IO;

class Program

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\Result.dat”;

        try

        {

            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))

            {

                byte[] data = new byte[] { 118, 99, 97, 110, 104, 101, 108, 112, 115, 117 };  // File data is – (Vcanhelpsu)

                // here it Write data to the result file

                fs.Write(data, 0, data.Length);

                Console.WriteLine(“user define file Data successfully written to the file.”);

            }

        }

        catch (IOException excption)

        {

            Console.WriteLine(“file written data error occurred. ” + excption.Message);

        }

    }

}

Example writing a single byte.

C# यूजर फाइल हैंडलिंग प्रोग्राम में WriteByte() फंक्शन मेथड का यूज़ करके अलग-अलग फाइल में बाइट इनफार्मेशन भी राइट कर सकते हैं।

using System;

using System.IO;

class Program

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\Result.dat”;

        try

        {

            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))

            {

                fs.WriteByte(118);  // here we Write byte representing to ‘V’ character

                Console.WriteLine(“Byte written successfully to the result file.”);

            }

        }

        catch (IOException excption)

        {

            Console.WriteLine(” error occurred during file operation. ” + excption.Message);

        }

    }

}

Seeking and positioning concepts in FileStream.

C# यूजर फाइल स्ट्रीम के अंदर फाइल पोजिशन को मूव करने के लिए Seek() फंक्शन मेथड का यूज़ कर सकते हैं। Seek() फंक्शन का यूज़ तब होता है, जब C# यूजर मौजूदा फाइल को रीड या राइट करने से पहले फ़ाइल में किसी पर्टिकुलर स्पेशल लोकेशन पर नेविगेट करना चाहते हैं।

Example of moving to a specific file location in a file.

using System;

using System.IO;

class Program

{

    static void Main()

    {

        string filePath = @”C:\path\filedata\myfile\Result.dat”;

        try

        {

            // here it Check if file exists or not

            if (!File.Exists(filePath))

            {

                Console.WriteLine(“Search File is not found.”);

                return;

            }

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))

            {

                // here it Check file length before seeking

                if (fs.Length <= 7)

                {

                    Console.WriteLine(“Here This File does not contain enough for bytes.”);

                    return;

                }

                // here it Move to the 7th byte position in result file

                fs.Seek(7, SeekOrigin.Begin);

                // here it Read byte after file seeking

                int byteRead = fs.ReadByte();

                if (byteRead != -1)

                {

                    Console.WriteLine($”Here the Byte at position 7 is – {byteRead}”);

                    Console.WriteLine($”Byte Character is – {(char)byteRead}”);

                }

                else

                {

                    Console.WriteLine(“Here No file byte could be read.”);

                }

            }

        }

        catch (IOException excption)

        {

            Console.WriteLine(“File system I/O Error-  ” + excption.Message);

        }

        catch (UnauthorizedAccessException)

        {

            Console.WriteLine(“File seek Access denied.”);

        }

        catch (Exception excption)

        {

            Console.WriteLine(“Unexpected file Error display. ” + excption.Message);

        }

    }

}

Asynchronous I/O operations with FileStream.

C# फाइल हैंडलिंग में लार्ज फ़ाइलों के लिए मेन थ्रेड को ब्लॉक होने से अवॉयड करने के लिए ज्यादातर एसिंक्रोनस I/O फाइल ऑपरेशन का यूज़ करना एक बेहतर ऑप्शन होता है। जो इसके यूजर को FileStream, ReadAsync() और WriteAsync() जैसे फाइल हैंडलिंग मेथड के एसिंक्रोनस वर्शन प्रोवाइड करता है।

Example of asynchronous file reading.

using System;

using System.IO;

using System.Text;

using System.Threading.Tasks;

class Program

{

    static async Task Main()

    {

        string filePath = @”C:\path\filedata\myfile\Result.dat”;

        try

        {

            // here it Check if file exists or not

            if (!File.Exists(filePath))

            {

                Console.WriteLine(“Search File not found.”);

                return;

            }

            using (FileStream fs = new FileStream(

                filePath,

                FileMode.Open,

                FileAccess.Read,

                FileShare.Read,

                1024,

                true)) // here it Enable async file operation mode

            {

                byte[] buffer = new byte[1024];

                int bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                Console.WriteLine($”Read file data {bytesRead} bytes asynchronously manner.”);

                // here it Convert bytes to text

                string content = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                Console.WriteLine(“File Content is.”);

                Console.WriteLine(content);

            }

        }

        catch (IOException excption)

        {

            Console.WriteLine(“I/O file operation Error. ” + excption.Message);

        }

        catch (UnauthorizedAccessException)

        {

            Console.WriteLine(“file Access denied.”);

        }

        catch (Exception excption)

        {

            Console.WriteLine(“Unexpected file processing Error. ” + excption.Message);

        }

    }

}

Closing a FileStream in File Handling.

फाइल हैंडलिंग में FileStream क्लास IDisposable को इम्प्लीमेंट करता है, इसका मतलब है कि आपके सभी फाइल हैंडलिंग ऑपरेशन कम्पलीट होने पर C# यूजर को इसे हमेशा ठीक से क्लोज या डिस्पोज़ करना चाहिए। वही using फाइल स्टेटमेंट ऑटोमैटिकली मौजूदा फाइल FileStream को क्लोज करने में हेल्प करता है. जब आपका फाइल हैंडलिंग प्रोग्राम प्रोग्राम कोड का ब्लॉक एग्जीक्यूशन टर्मिनेट करता है. चाहे यहाँ फाइल में कोई एक्सेप्शन हो या नहीं।

Conclusion of the FileStream Class.

  • फाइल हैंडलिंग प्रोसेस में FileStream C# प्रोग्राम में बाइट लेवल पर फ़ाइल ऑपरेशन  के साथ वर्क करने का सबसे बेसिक और एक कॉमन मेथड है। यह C# यूजर को बाइनरी और टेक्स्ट फ़ाइलों के साथ-साथ लार्ज फ़ाइलों को हैंडल करने के लिए कई फंक्शन मेथड और फीचर्स प्रोवाइड करता है।
  • FileStream फाइल हैंडलिंग फीचर्स StreamReader और StreamWriter मेथड की तुलना में ज़्यादा रिलाएबल फ़्लेक्सिबल और डिटेल्ड लोअर-लेवल मेथड है. जो किसी फाइल हैंडलिंग में टेक्स्ट-बेस्ड फाइल ऑपरेशन के लिए ऑप्टिमाइज़ किए गए हैं।
  • FileStream फाइल हैंडलिंग में इस फीचर्स यूज़ तब करें। जब C# यूजर को फाइल पर डिटेल्ड कंट्रोल चाहिए कि डेटा को फ़ाइल में कैसे रीड और राइट किये जाए, स्पेशली जब C# यूजर नॉन-टेक्स्ट फ़ाइलों के साथ काम कर तहा हो.

Leave a Reply