在 C# 中,檔案處理需引入`using System.IO`
使用 BinaryReader
和 BinaryWriter
來讀取和寫入二進位檔案
BinaryReader:
public BinaryReader (System.IO.Stream input);
BinaryWriter:
public BinaryWriter (System.IO.Stream output);
程式範例:
string filePath = "D:\\binary.dat";
try
{
// 寫入二進位檔案
using (BinaryWriter writer =
new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
writer.Write(42);
writer.Write("Hello, Binary!");
}
// 讀取二進位檔案
using (BinaryReader reader =
new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int number = reader.ReadInt32();
string text = reader.ReadString();
Console.WriteLine($"Number: {number}, Text: {text}");
}
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access);
:
程式範例:
string filePath = "D:\\advanced.txt";
try
{
// 使用 FileStream 進行檔案操作
using (FileStream fs =
new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
// 在檔案末尾寫入資料
fs.Seek(0, SeekOrigin.End);
byte[] data =
System.Text.Encoding.UTF8.GetBytes("這是進階檔案操作。");
fs.Write(data, 0, data.Length);
// 讀取檔案
fs.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string text = System.Text.Encoding.UTF8.GetString(buffer);
Console.WriteLine(text);
}
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
System.IO.Compression
來進行檔案的壓縮和解壓縮操作。
程式範例(壓縮檔案):
string sourceFilePath = "D:\\advanced.txt";
string compressedFilePath = "D:\\compressedFile.gz";
try
{
// 壓縮檔案
using (FileStream sourceStream =
new FileStream(sourceFilePath, FileMode.Open))
{
using (FileStream compressedStream =
File.Create(compressedFilePath))
{
using (GZipStream compressionStream =
new GZipStream(compressedStream, CompressionMode.Compress))
{
sourceStream.CopyTo(compressionStream);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
程式範例(解壓縮並建新檔案):
string compressedFilePath = "D:\\compressedFile.gz";
try
{
// 解壓縮檔案
string decompressedFilePath = "D:\\decompressedFile.txt";
using (FileStream compressedStream =
new FileStream(compressedFilePath, FileMode.Open))
{
using (FileStream decompressedStream =
File.Create(decompressedFilePath))
{
using (GZipStream decompressionStream =
new GZipStream(compressedStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedStream);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
題目:
程式實作:
// 步驟1:創建二進位檔案並寫入數據
void WriteBinaryData(string filePath)
{
try
{
using (BinaryWriter writer =
new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
// 寫入數據
writer.Write(15);
writer.Write("鐵人競賽加油啊!!!。");
}
Console.WriteLine("已成功創建並寫入二進位檔案。");
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
}
// 步驟2:使用 FileStream 讀取二進位檔案的內容
void ReadBinaryData(string filePath)
{
try
{
using (FileStream fs =
new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
// 讀取二進位數據
using (BinaryReader reader = new BinaryReader(fs))
{
// 讀取整數
int number = reader.ReadInt32();
// 讀取字串
string text = reader.ReadString();
Console.WriteLine($"Number: {number}, Text: {text}");
}
}
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
}
// 步驟3:壓縮二進位檔案並保存為壓縮檔案
void CompressFile(string sourceFilePath, string compressedFilePath)
{
try
{
using (FileStream sourceStream =
new FileStream(sourceFilePath, FileMode.Open))
{
using (FileStream compressedStream =
File.Create(compressedFilePath))
{
using (GZipStream compressionStream =
new GZipStream(compressedStream, CompressionMode.Compress))
{
sourceStream.CopyTo(compressionStream);
}
}
}
Console.WriteLine("已成功壓縮檔案並保存為壓縮檔案。");
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
}
// 步驟4:解壓縮壓縮檔案並讀取解壓縮後的內容
void DecompressFile(string compressedFilePath)
{
try
{
// 解壓縮檔案
string decompressedFilePath = "D:\\decompressedFile.dat";
using (FileStream compressedStream =
new FileStream(compressedFilePath, FileMode.Open))
{
using (FileStream decompressedStream =
File.Create(decompressedFilePath))
{
using (GZipStream decompressionStream =
new GZipStream(compressedStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedStream);
}
}
}
// 讀取解壓縮後的內容
using (BinaryReader reader =
new BinaryReader(File.Open(decompressedFilePath, FileMode.Open)))
{
int number = reader.ReadInt32();
string text = reader.ReadString();
Console.WriteLine($"解壓縮後的數據 - Number: {number}, Text: {text}");
}
Console.WriteLine("已成功解壓縮檔案並讀取數據。");
}
catch (Exception e)
{
Console.WriteLine("處理檔案時出錯:" + e.Message);
}
}
// 步驟1:創建一個二進位檔案並寫入數據
string binaryFilePath = "D:\\binary.dat";
WriteBinaryData(binaryFilePath);
// 步驟2:使用 FileStream 讀取二進位檔案的內容
ReadBinaryData(binaryFilePath);
// 步驟3:壓縮二進位檔案並保存為壓縮檔案
string compressedFilePath = "D:\\compressedFile.gz";
CompressFile(binaryFilePath, compressedFilePath);
// 步驟4:解壓縮壓縮檔案並讀取解壓縮後的內容
DecompressFile(compressedFilePath);
Console.ReadKey();
以上的解法不是最好的,但可以依照上方的方法學習、練習,
並期望自己找到更好的方法,持續進步><
期望挑戰30天持續更新成功 ~ DAY15