FileStream类读取文件,写入文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _03FileStream类 { class Program { static void Main(string[] args) { //FileStream 读取文件; string fPathr = @"E:dotNetStudy2.txt"; FileStream fsRead = new FileStream(fPathr, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length); string s = Encoding.Default.GetString(buffer, 0, r); fsRead.Close(); fsRead.Dispose(); Console.WriteLine(s); Console.ReadKey(); //FileStream 写入文件; string fPathw = @"E:dotNetStudy1.txt"; using (FileStream fsWrite = new FileStream(fPathw, FileMode.Append, FileAccess.Write, FileShare.Read)) { string str = "看看我会不会把你给覆盖掉~~~!!!"; Byte[] buffer1 = Encoding.Default.GetBytes(str); fsWrite.Write(buffer1, 0, buffer1.Length); } Console.WriteLine("Add Success!!"); Console.ReadKey(); } } }