本文主要是介绍C#练习1、文件读写1.2FileStream类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
FileStream类(文件流)
//FileStream fsRead = new FileStream(@"D:\C#ExampleFile\ex.txt", FileMode.OpenOrCreate, FileAccess.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();===============将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源using (FileStream fsWrite = new FileStream(@"D:\C#ExampleFile\ex.txt",FileMode.OpenOrCreate,FileAccess.Write)){string str = "写入文件测试示例";byte[] buffer = Encoding.UTF8.GetBytes(str);fsWrite.Write(buffer,0,buffer.Length);}Console.WriteLine("OK");Console.ReadLine();
作业:循环连续读写大文件;
查找文件夹及文件是否存在;
==Directory静态类,操作文件夹
File Path FileStream StreamReader StreamWriter创建文件夹//Directory.CreateDirectory(@"d:\888\66\777");//Console.Write("OK");//Console.Read();删除文件夹//Directory.Delete(@"d:\123",true);//Console.Write("OK");//Console.ReadKey();//检测文件夹是否存在//bool IsExist = Directory.Exists(@"D:\\111");//if (IsExist == true)//Console.Write("the directory is exsit!");//else//Console.Write("Not find the directoy"); //Console.Write(Convert.ToString(IsExist));//Console.ReadKey();移动文件夹//string SourceDir = "D:\\111";//string TargetDir = "D:\\123\\234";Directory.Move(SourceDir, TargetDir); //不能跨盘符移动;TargetDir上一级目录123不存在时或已存在最后一级文件夹234时报异常//CopyFolder(SourceDir, TargetDir);//string[] path=Directory.GetFiles(@"d:\pic","*.jpg"); 获得指定文件夹下所有文件的全路径//string[] path=Directory.GetDirectories(@"d:\111"); 获得指定目录下所有文件夹的全路径//for (int i = 0; i < path.Length; i++)//{// Console.WriteLine(path[i]);//}//Console.ReadKey();
Application.StartupPath 获取程序启动路径
System.Environment.CurrentDirectory 获取或设置当前工作路径,比如程序打开了E:文件夹AAA,此时取值就是E:AAA,而不是程序exe所在路径。
这篇关于C#练习1、文件读写1.2FileStream类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!