## 一、文件操作基礎
在C#中,文件操作主要通過`System.IO`命名空間中的類來實現,例如`File`、`FileStream`、`FileInfo`等。
## 二、常用文件操作方法
### (一)文件讀取
1. **使用`File.ReadAllText`方法讀取文件內容為字符串**
? ? ```
? ? string content = File.ReadAllText("example.txt", Encoding.UTF8);
? ? Console.WriteLine(content);
? ? ```
2. **使用`File.ReadAllLines`方法讀取文件內容為字符串數組**
? ? ```
? ? string[] lines = File.ReadAllLines("example.txt", Encoding.UTF8);
? ? foreach (string line in lines)
? ? {
? ? ? ? Console.WriteLine(line);
? ? }
? ? ```
3. **使用`FileStream`讀取文件**
? ? ```
? ? FileStream file = new FileStream("example.txt", FileMode.Open, FileAccess.Read);
? ? byte[] buffer = new byte[1024 * 1024 * 5]; // 每次讀取5M的數據
? ? int bytesRead = file.Read(buffer, 0, buffer.Length);
? ? string str = Encoding.UTF8.GetString(buffer, 0, bytesRead);
? ? Console.WriteLine(str);
? ? file.Close();
? ? file.Dispose();
? ? ```
### (二)文件寫入
1. **使用`File.WriteAllText`方法創建或覆蓋文件并寫入文本**
? ? ```
? ? string filePath = "example.txt";
? ? string textToWrite = "Hello, World!";
? ? File.WriteAllText(filePath, textToWrite, Encoding.UTF8);
? ? ```
2. **使用`File.AppendAllText`方法追加文本到文件**
? ? ```
? ? File.AppendAllText("example.txt", "追加的文本內容\n", Encoding.UTF8);
? ? ```
3. **使用`FileStream`寫入文件**
? ? ```
? ? FileStream fileStream = File.Create("example.txt");
? ? byte[] content = Encoding.UTF8.GetBytes("寫入內容");
? ? fileStream.Write(content, 0, content.Length);
? ? fileStream.Close();
? ? ```
### (三)文件創建
1. **使用`File.Create`方法創建文件**
? ? ```
? ? string filePath = @"c:\myFile.txt";
? ? FileStream fileStream = File.Create(filePath);
? ? fileStream.Close();
? ? ```
### (四)文件復制
1. **使用`File.Copy`方法復制文件**
? ? ```
? ? string sourceFilePath = @"c:\myFile.txt";
? ? string destinationFilePath = @"d:\myFile_copy.txt";
? ? File.Copy(sourceFilePath, destinationFilePath);
? ? ```
### (五)文件移動
1. **使用`File.Move`方法移動文件**
? ? ```
? ? string sourceFilePath = @"c:\myFile.txt";
? ? string destinationFilePath = @"d:\myFile.txt";
? ? File.Move(sourceFilePath, destinationFilePath);
? ? ```
### (六)文件刪除
1. **使用`File.Delete`方法刪除文件**
? ? ```
? ? string filePath = @"d:\myFile.txt";
? ? File.Delete(filePath);
? ? ```
### (七)判斷文件是否存在
1. **使用`File.Exists`方法判斷文件是否存在**
? ? ```
? ? bool exists = File.Exists("example.txt");
? ? Console.WriteLine(exists ? "文件存在" : "文件不存在");
? ? ```
## 三、`FileInfo`類
`FileInfo`類提供了對文件的高級操作功能,以下是一些示例:
### (一)讀取文件
```
FileInfo fileInfo = new FileInfo("example.txt");
if (fileInfo.Exists)
{
? ? using (FileStream fs = fileInfo.OpenRead())
? ? {
? ? ? ? byte[] buffer = new byte[fs.Length];
? ? ? ? int bytesRead = fs.Read(buffer, 0, buffer.Length);
? ? ? ? Console.WriteLine($"讀取了 {bytesRead} 字節的數據。");
? ? }
}
else
{
? ? Console.WriteLine("文件不存在。");
}
```
### (二)復制文件
```
FileInfo sourceFile = new FileInfo("source.txt");
string destinationPath = "destination.txt";
if (sourceFile.Exists)
{
? ? FileInfo copiedFile = sourceFile.CopyTo(destinationPath, false); // 不允許覆蓋
? ? Console.WriteLine($"文件已復制到: {copiedFile.FullName}");
}
else
{
? ? Console.WriteLine("源文件不存在。");
}
```
## 四、注意事項
1. 在進行文件操作時,需要注意文件的路徑是否正確,以及是否有足夠的權限訪問文件。
2. 使用`FileStream`等流操作文件時,記得在操作完成后關閉流并釋放資源,可以使用`using`語句來自動管理資源。
3. 在讀取或寫入文件時,要注意文件的編碼格式,避免出現亂碼問題。