using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FileSetream类
{
internal class Program
{
static void Main(string[] args)
{
#region 读取数据
//实例化 读取路径 如果没有该路径就创建 读取
// FileStream fsRead = new FileStream(@"C:\Users\Administrator\Desktop\IO操作\111111.txt",FileMode.OpenOrCreate,FileAccess.Read);
// //创建读取的解码的数组
// byte[] buffer = new byte[1024*1024*5];
返回本次实际读取到的有效字节
// int r =fsRead.Read(buffer,0,buffer.Length);
// //将字节数组中的每一个元素按照指定的编码格式解码成字符串
// string s=Encoding.UTF8.GetString(buffer,0,r);
// //关闭流
// fsRead.Close();
// //释放流所占用的资源
// fsRead.Dispose();
// Console.WriteLine(s);
// Console.ReadKey();
#endregion
#region 写入数据
using (FileStream fsWrite = new FileStream(@"C:\Users\Administrator\Desktop\IO操作\222222.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
string str = "看我把你干掉1111";
//定义编码
byte[] buffer = Encoding.UTF8.GetBytes(str);
//将位置赋值,使其追加
fsWrite.Position = buffer.Length;
fsWrite.Write(buffer, 0, buffer.Length);
}
Console.WriteLine("写入成功");
Console.ReadKey();
#endregion
}
}
}
FileStream类操作
最新推荐文章于 2024-10-06 15:50:38 发布