Stream เป็น Abstract Base Class แทน Sequence of Bytes — รองรับ Read/Write/Seek แบบ Sequential หรือ Random Access
Stream Hierarchy
Stream (Abstract)
├── FileStream // ไฟล์บน Disk
├── MemoryStream // Byte Array ใน Memory
├── BufferedStream // Wrapper เพิ่ม Buffering
├── NetworkStream // Network Socket
├── CryptoStream // Encryption/Decryption
├── GZipStream / DeflateStream // Compression
└── PipeStream // Inter-process Communication
Byte Streams vs Character Streams
| Byte Streams | Character Streams (Text) |
| Stream, FileStream, MemoryStream, BufferedStream | StreamReader, StreamWriter |
| BinaryReader, BinaryWriter | StringReader, StringWriter |
| จัดการ Raw Bytes | จัดการ Text (Encoding Aware) |
| Read/Write Byte[] | Read/Write String/Char |
Stream Properties
| Property | คำอธิบาย |
CanRead | รองรับ Read หรือไม่ |
CanWrite | รองรับ Write หรือไม่ |
CanSeek | รองรับ Seek (Position) หรือไม่ |
CanTimeout | รองรับ Timeout หรือไม่ |
Length | ความยาว Stream (Bytes) |
Position | ตำแหน่งปัจจุบัน (Get/Set) |
System.IO Classes สำคัญ
| Class | Description |
FileStream | Read/Write ไฟล์บน Disk (Random Access) |
MemoryStream | Stream ใน RAM (Byte[]), เร็วมาก |
BufferedStream | Wrap Stream อื่นเพิ่ม Buffer (ลด System Calls) |
BinaryReader/Writer | Read/Write Primitive Types (int, float, bool, string) |
StreamReader/Writer | Read/Write Text (Encoding: UTF-8 Default) |
StringReader/Writer | Read/Write จาก String ใน Memory |
File | Static Helpers: ReadAllText, WriteAllBytes, Exists, Copy, Delete |
FileInfo | Instance Methods สำหรับ File Operations |
Directory / DirectoryInfo | Directory Operations |
Path | Path Manipulation (Combine, GetExtension, GetFileName) |
DriveInfo | Drive Information (Free Space, Format) |
ตัวอย่าง: FileStream + BinaryReader/Writer
using System;
using System.IO;
class BinaryDemo {
static void Main() {
string path = "data.bin";
// Write
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
using (var bw = new BinaryWriter(fs)) {
bw.Write(42); // int
bw.Write(3.14); // double
bw.Write(true); // bool
bw.Write("Hello"); // string (Length-prefixed)
}
// Read
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(fs)) {
int i = br.ReadInt32();
double d = br.ReadDouble();
bool b = br.ReadBoolean();
string str = br.ReadString();
Console.WriteLine($"{i}, {d}, {b}, {str}");
}
}
}
ตัวอย่าง: StreamReader/Writer (Text)
using System.IO;
string path = "notes.txt";
// Write Lines
using (var sw = new StreamWriter(path, append: false, System.Text.Encoding.UTF8)) {
sw.WriteLine("Line 1");
sw.WriteLine("Line 2");
}
// Read All Lines
string[] lines = File.ReadAllLines(path);
// Read Line by Line (Memory Efficient)
using (var sr = new StreamReader(path)) {
string line;
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
Best Practices
- ✅ ใช้
using / await using — ปิด Stream อัตโนมัติ (Dispose)
- ✅ ใช้
File.ReadAllText/WriteAllText สำหรับไฟล์เล็ก — สะดวก
- ✅ ใช้
StreamReader/Writer สำหรับไฟล์ใหญ่ — Stream ไม่โหลดทั้งไฟล์เข้า RAM
- ✅ ระบุ
Encoding ชัดเจน (UTF-8 Default ใน .NET Core)
- ❌ อย่าเก็บ Stream ไว้นาน — ปล่อย Resource ทันที
- ❌ อย่า Mix Byte Stream กับ Text Stream บน File เดียวกันพร้อมกัน