I\O流主要有:Stream FileStream、StreamRead、StreamWrite。
Stream 是所有流的抽象基類
流涉及三個基本操作:
1. 可以讀取流。讀取是從流到數據結構(如字節數組)的數據傳輸。
2. 可以寫入流。寫入是從數據結構到流的數據傳輸。
3. 流可以支持查找。查找是對流內的當前位置進行查詢和修改。查找功能取決於流具有的後備存儲區類型。
字符連接 static void Main(string[] args)
{
string path1 = @"D:\demo\test1.txt";//文本路徑
FileStream fs1 = new FileStream(path1, FileMode.Open);//創建文件流
Byte[] By_Arr1=new Byte[fs1.Length];
fs1.Read(By_Arr1,0,By_Arr1 .Length);//讀出第一個文件
string path2 = @"D:\demo\test2.txt";
FileStream fs2 = new FileStream(path2, FileMode.Open);
Byte[] By_Arr2 = new Byte[fs2.Length];
fs2.Read(By_Arr2, 0, By_Arr2.Length);
fs2.SetLength(By_Arr1.Length + By_Arr2.Length); //設置文件長度
fs2.Write(By_Arr2 ,0,By_Arr2 .Length );}//讀出新文件
StreamReader 旨在以一種特定的編碼輸入字符,而 Stream 類用於字節的輸入和輸出。
StreamReader sr = new StreamReader(@"D:\demo\test1.txt", Encoding.Default);//路徑和字符轉換
string str;
//while ((str = sr.ReadLine()) != null)
//{
// Console.WriteLine(str);
//}
Console.WriteLine(str = sr.ReadToEnd());
sr.Close();
StreamWriter 旨在以一種特定的編碼輸出字符
StreamWriter sw = new StreamWriter(@"D:\demo\test2.txt", true, Encoding.Default);
sw.Write(str + "在這裏增加新的內容");
sw.Close();