কম্পিউটার টিউটোরিয়াল

C#-এ ফাইল পড়ার বিভিন্ন পদ্ধতি

এখানে আমরা দুটি ভিন্ন ফাইল পড়ছি:

টেক্সট ফাইল পড়া

উদাহরণ

using System;
using System.IO;

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (StreamReader sr = new StreamReader("d:/new.txt"))
                {
                    string line;
                    // ফাইল থেকে লাইন পড়ুন এবং প্রদর্শন করুন
                    // ফাইল শেষ পর্যন্ত
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // ব্যবহারকারীকে জানান কী ভুল হয়েছে
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}

আউটপুট

The file could not be read: Could not find a part of the path "d:/new.txt".

বাইনারি ফাইল পড়া

উদাহরণ

using System.IO;

namespace BinaryFileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            BinaryWriter bw;
            BinaryReader br;
            int i = 25;
            double d = 3.14157;
            bool b = true;
            string s = "I am happy";
            // ফাইল তৈরি
            try
            {
                bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot create file.");
                return;
            }
            // ফাইলে লেখা
            try
            {
                bw.Write(i);
                bw.Write(d);
                bw.Write(b);
                bw.Write(s);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot write to file.");
                return;
            }
            bw.Close();
            // ফাইল থেকে পড়া
            try
            {
                br = new BinaryReader(new FileStream("mydata", FileMode.Open));
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot open file.");
                return;
            }
            try
            {
                i = br.ReadInt32();
                Console.WriteLine("Integer data: {0}", i);
                d = br.ReadDouble();
                Console.WriteLine("Double data: {0}", d);
                b = br.ReadBoolean();
                Console.WriteLine("Boolean data: {0}", b);
                s = br.ReadString();
                Console.WriteLine("String data: {0}", s);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot read from file.");
                return;
            }
            br.Close();
            Console.ReadKey();
        }
    }
}
  1. Mac-এ ফাইল পাথ দেখানোর ৫টি উপায়

  2. Bash-এ ফাইল পড়া মাস্টার করুন: সেরা While-Loop টেকনিকসমূহ

  3. ম্যাক থেকে Instagram-এ ডায়রেক্ট মেসেজ পাঠানোর সহজ ধাপে ধাপে গাইড

  4. Windows 11-এ ফাইল আনজিপ করার ৩টি সহজ পদ্ধতি