Skip to main content

Chapter 5.1: File I/O and Serialization: Understanding Reading and Writing Text and Binary Files

 Introduction:

In this new chapter, we'll explore File I/O and Serialization, essential concepts for working with data in C#. In this first article, we'll discuss reading and writing text and binary files. In the next article, we'll delve into serialization and deserialization of objects, which are important for persisting and restoring object states.


Reading and Writing Text Files:

C# provides several classes for reading and writing text files, such as StreamReader, StreamWriter, and File:

// Writing a text file
using (StreamWriter writer = new StreamWriter("example.txt"))
{
    writer.WriteLine("Hello, world!");
}

// Reading a text file
using (StreamReader reader = new StreamReader("example.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}


Reading and Writing Binary Files:

For working with binary files, C# provides the BinaryReader and BinaryWriter classes:

// Writing a binary file
using (BinaryWriter writer = new BinaryWriter(File.Open("example.bin", FileMode.Create)))
{
    writer.Write(42);
    writer.Write("Hello, world!");
}

// Reading a binary file
using (BinaryReader reader = new BinaryReader(File.Open("example.bin", FileMode.Open)))
{
    int number = reader.ReadInt32();
    string text = reader.ReadString();

    Console.WriteLine(number);
    Console.WriteLine(text);
}


File Handling Best Practices:

When working with files, it's essential to follow best practices to ensure proper resource management and error handling:

  • Use the using statement to automatically close and dispose of file resources.
  • Use exception handling (try-catch blocks) to manage potential errors, such as file not found or access denied.

Conclusion:

In this article, we discussed reading and writing text and binary files, essential skills for working with data in C#. Understanding these concepts will help you handle data effectively in your applications. In the next article, we'll delve into serialization and deserialization of objects, important for persisting and restoring object states. Stay tuned for more insights into File I/O and Serialization!

Comments

Some of My Bests

ডাটা স্ট্রাকচার- স্ট্যাক (Stack)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা স্ট্যাক বুঝানোর জন্য। স্ট্যাক খুবই ইন্টেরেস্টিং একটা ডাটা স্ট্রাকচার। অনেক কারনেই এটা ব্যাবহার হয়। আগে স্ট্যাক কনসেপ্টটা নিয়ে আলোচনা করা যাক। স্ট্যাক বলতে বোঝায় একটার উপর একটা সাজায়ে রাখা। বিয়ের বাড়িতে আগে মেলামাইনের গ্লাস দেখা যেত একটার ভেতর আরেকটা ঢুকায়ে লম্বা একটা পাইল তৈরি করে একসাথে ক্যারি করা হচ্ছে। এটা একটা স্ট্যাক। প্রোগ্রামিং এর ভাষায় স্ট্যাক এক্সাক্টলি সেইম জিনিসই। তবে একটু ঘষামাজা আছে। আরেকটা উদাহরণ দেয়া যেতে পারে। বয়ামের ভেতর একটার পর আরেকটা বিস্কিট ঢুকিয়ে রাখা হয়। হ্যা, এইটা পারফেক্ট উদাহরণ হয়েছে। এই

ডাটা স্ট্রাকচার- কিউ (Queue)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা কিউ বুঝানোর জন্য। কিউ জিনিসটার সাথে আমরা সবাই পরিচিত। জীবনে আমরা সবাই কখনো না কখনো লাইনে দাঁড়ায়ছি। কিউ এর বেসিক ক্যারেক্টারিস্টিকসের সাথে মিলিয়েই প্রোগ্রামিং এ কিউ এর কনসেপ্ট। বাস্তব জীবনে একটা কিউ তে কি হয়? সবাই লাইন ধরে দাঁড়ায় কিছু একটা কারনে। যে সবার আগে দাঁড়ায় সেই সবার আগে কার্জসিদ্ধি করে। সবার পরের জন সবার পরে। স্ট্যাকের ক্ষেত্রে আমরা পড়েছিলাম Last In First Out (LIFO) or First In Last Out (FILO)। কেমন আনফেয়ার শোনায় না? সবার পরে আসবে, আবার সবার আগে চলে যাবে। অ্যাটলিস্ট আমার আনফেয়ার লেগেছিলো যখন স্ট্যাক শিখছিল

কম্পিউটার বচন

কম্পিউটারের সাথে আমার পরিচয় খুব ছোট বয়সে না। অন্তত চেনাপরিচিত বন্ধুমহলের অনেকের তুলনায় আমি বাচ্চাই বলা চলে। ক্লাস ২ বা ৩ তে পড়ি যখন ফুফাতো ভাই প্রথম কম্পিউটার কেনে। ফুফুর বাসা কাছেই হওয়ায় মাঝে মাঝেই সেটা দেখার সৌভাগ্য হতো। কিন্তু ছুয়ে দেখার সাহস তখনও হয়নি। সেই বছর থেকে আমাদের রেওয়াজ, ঈদের দিন নামাজ পড়েই সোজা ভাইয়ার রুমে। ভাইয়া নতুন একটা গেম ইন্সটল করে রাখতো আর আমরা কাজিনেরা লাইন দিয়ে সেটা খেলতাম (যদিও আমার ভাগে কমই পড়তো :p )  ক্লাস সিক্সে নতুন স্কুলে ভর্তি হলাম। এ এক আজব জায়গা। সবাই বড়লোকের পোলাপাইন। কতজন গাড়িতে করে আসে। তাদের তো কথাই নাই। যাদের গাড়ি নাই তাদেরও রোজকার বাজেট বিশাল। ইচ্ছামত উড়ায়। আর আমি মধ্যবিত্তের ছেলে। যাওয়া আসার ভাড়া বাদে কোনো টাকা পেতামনা। টিফিন বাসা থেকে বানায়ে দিতো। এই যখন আমার অবস্থা তখন ক্লাসমেটরা নিত্যনতুন গ্যাজেটের সাথে শুধু পরিচয়ই না, পকেটে নিয়ে ঘুরে (যদিও স্কুলে নিষিদ্ধ ছিলো )। সবাই এসব নিয়ে কত গল্প চালায় যায়। আর আমার মত বোকা কিছু পাবলিক হা করে এদের গল্প শুনি। কম্পিউটারের কত বচন, কত আড্ডা। এসব আড্ডায় ঢুকতে কত ইচ্ছা করতো, কিন্তু ঢুকবো কি করে? আমার কম্পিউটা