IO概述 #

一、IO体系结构 #

1.1 流的概念 #

流是字节序列的抽象,提供读写数据的能力。

1.2 IO类层次 #

text
System.IO
├── Stream (抽象基类)
│   ├── FileStream
│   ├── MemoryStream
│   ├── NetworkStream
│   └── BufferedStream
├── TextReader/TextWriter
│   ├── StreamReader/StreamWriter
│   └── StringReader/StringWriter
├── BinaryReader/BinaryWriter
├── File/FileInfo
├── Directory/DirectoryInfo
└── Path

二、Stream类 #

2.1 基本属性和方法 #

csharp
public abstract class Stream : IDisposable
{
    public abstract bool CanRead { get; }
    public abstract bool CanWrite { get; }
    public abstract bool CanSeek { get; }
    public abstract long Length { get; }
    public abstract long Position { get; set; }
    
    public abstract int Read(byte[] buffer, int offset, int count);
    public abstract void Write(byte[] buffer, int offset, int count);
    public abstract long Seek(long offset, SeekOrigin origin);
    public abstract void Flush();
    public abstract void SetLength(long value);
    
    public virtual void CopyTo(Stream destination);
    public virtual Task CopyToAsync(Stream destination);
}

2.2 FileStream #

csharp
using var fs = new FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] data = Encoding.UTF8.GetBytes("Hello");
fs.Write(data, 0, data.Length);

fs.Position = 0;
byte[] buffer = new byte[1024];
int bytesRead = fs.Read(buffer, 0, buffer.Length);

2.3 MemoryStream #

csharp
using var ms = new MemoryStream();
byte[] data = Encoding.UTF8.GetBytes("Hello");
ms.Write(data, 0, data.Length);

ms.Position = 0;
byte[] result = ms.ToArray();

三、文本读写 #

3.1 StreamReader #

csharp
using var reader = new StreamReader("data.txt");
string content = reader.ReadToEnd();

using var reader = new StreamReader("data.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
    Console.WriteLine(line);
}

3.2 StreamWriter #

csharp
using var writer = new StreamWriter("output.txt");
writer.WriteLine("第一行");
writer.WriteLine("第二行");

using var writer = new StreamWriter("output.txt", append: true);
writer.WriteLine("追加内容");

四、二进制读写 #

4.1 BinaryWriter #

csharp
using var writer = new BinaryWriter(File.OpenWrite("data.bin"));
writer.Write(42);
writer.Write(3.14);
writer.Write("Hello");

4.2 BinaryReader #

csharp
using var reader = new BinaryReader(File.OpenRead("data.bin"));
int number = reader.ReadInt32();
double pi = reader.ReadDouble();
string text = reader.ReadString();

五、File类 #

5.1 文件操作 #

csharp
string content = File.ReadAllText("data.txt");
string[] lines = File.ReadAllLines("data.txt");
byte[] bytes = File.ReadAllBytes("data.bin");

File.WriteAllText("output.txt", "Hello World");
File.WriteAllLines("output.txt", new[] { "Line1", "Line2" });
File.WriteAllBytes("output.bin", bytes);

File.AppendAllText("log.txt", "New log entry\n");

File.Copy("source.txt", "dest.txt", overwrite: true);
File.Move("old.txt", "new.txt");
File.Delete("temp.txt");

bool exists = File.Exists("data.txt");

六、Directory类 #

6.1 目录操作 #

csharp
Directory.CreateDirectory("newFolder");

string[] files = Directory.GetFiles("folder");
string[] dirs = Directory.GetDirectories("folder");

Directory.Move("oldPath", "newPath");
Directory.Delete("folder", recursive: true);

bool exists = Directory.Exists("folder");

string current = Directory.GetCurrentDirectory();
string[] logicalDrives = Directory.GetLogicalDrives();

七、Path类 #

7.1 路径操作 #

csharp
string fullPath = Path.Combine("folder", "subfolder", "file.txt");
string fileName = Path.GetFileName("folder/file.txt");
string dirName = Path.GetDirectoryName("folder/file.txt");
string ext = Path.GetExtension("file.txt");
string nameNoExt = Path.GetFileNameWithoutExtension("file.txt");

string tempPath = Path.GetTempPath();
string tempFile = Path.GetTempFileName();
string randomName = Path.GetRandomFileName();

八、总结 #

IO要点:

用途
Stream 字节流基类
FileStream 文件流
StreamReader/Writer 文本读写
BinaryReader/Writer 二进制读写
File 文件操作
Directory 目录操作
Path 路径操作

下一步,让我们学习文件操作详解!

最后更新:2026-03-26