using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Filestream
{
/// <summary>
/// 从流中读,向流中写
/// </summary>
class Program
{
static void Main(string[] args)
{
string path = @"c:\temp\MyText.txt";
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path))//using自动释放资源
{
AddText(fs, "hello beijing");
AddText(fs, "2008");
}
using (FileStream fs = File.OpenRead(path))//将新建的文件作为流的数据源或者结束源
{
byte[] b = new byte[1024];
UTF8Encoding temp= new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)//从流中一次读取一个块,存入缓冲区中
{
Console.WriteLine(temp.GetString(b));
}
}
}
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);//向流中写入,一次写入一个块
}
}
}