CACS408 Dot Net Technology

Dot Net TechnologyUnit 910 min read

File Handling & Serialization in C: Streams, Formats & Data Persistence

Unit 9 of Dot Net Technology covers file I/O operations (text/binary), serialization (XML, JSON, binary), stream classes (FileStream, StreamReader), and exception handling in file operations. Learn how to read/write files, serialize objects, and handle errors—with real-world examples from eSewa, Daraz, and banking syst

TAKEAWAYS:

  • File handling in C# uses streams (FileStream, StreamReader) to read/write data sequentially or randomly.
  • Serialization converts objects to/from files (XML, JSON, binary) for persistence or network transfer.
  • Text files store human-readable data (e.g., .txt, .csv), while binary files store raw bytes (e.g., .dat, images).
  • Exception handling (try-catch) is critical for file operations (e.g., FileNotFoundException, IOException).
  • Serialization attributes ([Serializable], [XmlElement]) control how objects are saved/loaded.
  • Real-world use: eSewa serializes transaction logs, Daraz stores order data in binary files, and banks use XML for loan agreements.

1. File Handling in C#

Files store data persistently. C# provides classes to interact with files via streams (sequential or random access).

Key Classes for File Handling

Class Purpose
FileStream Base class for file I/O (supports binary/text modes).
StreamReader/StreamWriter Reads/writes text files (UTF-8, Unicode).
BinaryReader/BinaryWriter Reads/writes binary data (e.g., integers, custom objects).
File (static class) Provides methods like File.ReadAllText(), File.Exists().

Text vs. Binary Files

mindmap
  root((File Types))
    Text Files
      "Human-readable (e.g., .txt, .csv)"
      "Slower, larger size"
      "Example: Log files, config files"
    Binary Files
      "Raw bytes (e.g., .dat, images)"
      "Faster, smaller size"
      "Example: Database records, serialized objects"

Example: Copying a Text File

Task: Copy contents of source.txt to destination.txt using StreamReader/StreamWriter.

using System;
using System.IO;

class Program {
    static void Main() {
        string sourcePath = @"C:\data\source.txt";
        string destPath = @"C:\data\destination.txt";

        try {
            // Create readers/writers
            using (StreamReader reader = new StreamReader(sourcePath))
            using (StreamWriter writer = new StreamWriter(destPath)) {
                string line;
                while ((line = reader.ReadLine()) != null) {
                    writer.WriteLine(line); // Copy line-by-line
                }
            }
            Console.WriteLine("File copied successfully!");
        }
        catch (FileNotFoundException) {
            Console.WriteLine("Source file not found.");
        }
        catch (IOException ex) {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

Trace Table:

Step reader State writer State Action
Initialization Opened source.txt Opened destination.txt reader.ReadLine()
Line 1 Read At line 1 Writes "Hello" writer.WriteLine("Hello")
Line 2 Read At line 2 Writes "World" writer.WriteLine("World")
EOF null returned File closed Loop exits

Real-World Tie-In:

  • eSewa uses text files to log transactions (e.g., transaction_20240515.txt) for auditing.
  • Daraz stores order details in binary files for faster access during peak hours.

2. Binary File Handling

Binary files store data in raw bytes (e.g., integers, custom objects). Use BinaryReader/BinaryWriter.

Example: Saving Student Records

Task: Save a Student object to a binary file (student.dat) and read it back.

[Serializable]
class Student {
    public int Roll { get; set; }
    public string Name { get; set; }
    public float Marks { get; set; }
}

class Program {
    static void Main() {
        // Serialize to binary file
        Student student = new Student { Roll = 1, Name = "Ramesh", Marks = 85.5f };
        string filePath = @"C:\data\student.dat";

        using (FileStream stream = new FileStream(filePath, FileMode.Create))
        using (BinaryWriter writer = new BinaryWriter(stream)) {
            writer.Write(student.Roll);
            writer.Write(student.Name);
            writer.Write(student.Marks);
        }

        // Deserialize from binary file
        using (FileStream stream = new FileStream(filePath, FileMode.Open))
        using (BinaryReader reader = new BinaryReader(stream)) {
            int roll = reader.ReadInt32();
            string name = reader.ReadString();
            float marks = reader.ReadSingle();
            Console.WriteLine($"Roll: {roll}, Name: {name}, Marks: {marks}");
        }
    }
}

Binary File Structure (after writing):

| Roll (4 bytes) | Name (UTF-16, variable) | Marks (4 bytes) |
|----------------|-------------------------|------------------|
| 0x00000001     | 'R','a','m','e','s','h' | 0x40266666       |

Real-World Tie-In:

  • Ncell stores customer call logs in binary files for analytics (faster than text).
  • Banks use binary files to store loan agreements (compact and secure).

3. Serialization in C#

Serialization converts objects to a stream (file/network) and deserializes them back.

Types of Serialization

Type Format Use Case Example Attributes
Binary .dat Performance-critical apps [Serializable]
XML .xml Config files, web services [XmlElement], [XmlRoot]
JSON .json Web APIs, NoSQL databases System.Text.Json

Example: XML Serialization

Task: Serialize a Student object to XML (student.xml).

[Serializable]
[XmlRoot("Student")]
class Student {
    [XmlElement("Roll")]
    public int Roll { get; set; }
    [XmlElement("Name")]
    public string Name { get; set; }
}

class Program {
    static void Main() {
        Student student = new Student { Roll = 1, Name = "Ramesh" };
        string filePath = @"C:\data\student.xml";

        // Serialize to XML
        XmlSerializer serializer = new XmlSerializer(typeof(Student));
        using (FileStream stream = new FileStream(filePath, FileMode.Create))
            serializer.Serialize(stream, student);

        // Deserialize from XML
        using (FileStream stream = new FileStream(filePath, FileMode.Open))
            student = (Student)serializer.Deserialize(stream);

        Console.WriteLine($"Roll: {student.Roll}, Name: {student.Name}");
    }
}

Generated XML:

<Student>
  <Roll>1</Roll>
  <Name>Ramesh</Name>
</Student>

Real-World Tie-In:

  • NEPSE uses XML to serialize stock transaction data for regulatory compliance.
  • WhatsApp serializes chat messages to JSON for cloud backup.

4. Exception Handling in File Operations

File operations can fail due to:

  • Missing files (FileNotFoundException).
  • Permission issues (UnauthorizedAccessException).
  • Disk full (IOException).

Example: Handling File Exceptions

try {
    string content = File.ReadAllText(@"C:\nonexistent.txt");
}
catch (FileNotFoundException ex) {
    Console.WriteLine("File not found. Using default data.");
}
catch (IOException ex) {
    Console.WriteLine($"IO Error: {ex.Message}");
}
finally {
    Console.WriteLine("Operation attempted.");
}

Common Exceptions:

Exception Cause Solution
FileNotFoundException File does not exist Check path, create file if needed
DirectoryNotFoundException Directory missing Verify directory structure
UnauthorizedAccessException No read/write permissions Check file permissions
IOException Disk full, corrupted file Handle gracefully

5. Real-World Applications

Company/Product Use Case Serialization/File Type
eSewa Transaction logs Text files (.txt) + XML
Khalti Payment records Binary files (.dat)
Daraz Order processing JSON (for API) + Binary (DB)
NTC Customer billing data XML (for reporting)
Ncell Call detail records (CDRs) Binary (for analytics)
Banks (NMB, SBI) Loan agreements XML (for compliance)

Worked Example: Daraz Order Queue Daraz uses a priority queue (serialized to a binary file) to process orders:

  1. High-priority orders (e.g., "Same-Day Delivery") are written first.
  2. The queue is serialized to orders.dat using BinaryFormatter.
  3. Workers deserialize and process orders in FIFO order.
sequenceDiagram
    Customer->>Daraz: Place Order (HTTP)
    Daraz->>Queue: Add to PriorityQueue
    Queue->>File: Serialize to orders.dat
    Worker->>File: Deserialize orders.dat
    Worker->>Courier: Dispatch Order

Exam Tip

  1. File Handling Questions:

    • Always use using blocks to auto-close streams (avoids memory leaks).
    • For text files, use StreamReader/StreamWriter; for binary, use BinaryReader/BinaryWriter.
    • Common pitfalls:
      • Forgetting FileMode.Create (overwrites files).
      • Not handling IOException (e.g., disk full).
  2. Serialization Questions:

    • Mark objects with [Serializable] for binary serialization.
    • For XML/JSON, use attributes like [XmlElement].
    • Exam favorite: Serialize a custom class to a file and deserialize it back.
  3. Exception Handling:

    • Catch specific exceptions (e.g., FileNotFoundException) rather than generic Exception.
    • Use finally for cleanup (e.g., closing connections).
  4. Code Structure:

    • Start with using System.IO; and using System.Xml.Serialization;.
    • For binary serialization, implement ISerializable if needed.

Visual Summary:

flowchart TD
    A["File Handling"] --> B["Text Files<br/>StreamReader/Writer"]
    A --> C["Binary Files<br/>BinaryReader/Writer"]
    A --> D["Serialization<br/>XML/JSON/Binary"]
    D --> E["BinaryFormatter<br/>[Serializable]"]
    D --> F["XmlSerializer<br/>[XmlElement]"]
    D --> G["JsonSerializer<br/>System.Text.Json"]
    B --> H["Log Files<br/>Config Files"]
    C --> I["Database Records<br/>Custom Objects"]
    E --> J["Ncell Call Logs"]
    F --> K["NEPSE Stock Data"]
    G --> L["WhatsApp Backups"]

Based on the TU BCA syllabus for Dot Net Technology (CACS408), unit 9.

Discussion

Loading…