using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace 讀寫文件 { class Program { static void Ma ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 讀寫文件
{
class Program
{
static void Main(string[] args)
{
#region 讀取
FileStream fsRead = new FileStream(@"C:\Users\Administrator\Desktop\畢畢.txt", FileMode.OpenOrCreate, FileAccess.Read);
//定義位元組數組,每次讀取多少
byte[] buffer = new byte[1024 * 1024 * 5];
int r = fsRead.Read(buffer, 0, buffer.Length);//返回一共讀到的有效位元組數
// MessageBox.Show(r.ToString());
//將位元組數組中每一個元素按照指定的編碼格式解碼成位元組串
string s = Encoding.UTF8.GetString(buffer, 0, r);
//關閉流
fsRead.Close();
//釋放流所占用的資源
//實際上 還是讀了5兆 只是顯示了所讀到的那些
fsRead.Dispose();
Console.WriteLine(s);
Console.ReadLine();
#endregion
#region 寫入
//using (FileStream fswrite = new FileStream(@"C:\Users\Administrator\Desktop\畢畢.txt", FileMode.OpenOrCreate, FileAccess.Write))
//{
// //覆蓋,覆蓋所需占用的位元組量
// string str = "yes";
// byte[] buffer = Encoding.UTF8.GetBytes(str);
// fswrite.Write(buffer, 0, buffer.Length);
//}
#endregion
}
}
}