操作文件流的类,读写类StreamReader,StreamWriter和File类以及Directory类的操作

3/8/2017来源:ASP.NET技巧人气:3329

FileStream类的控制

(1) Flush();  清除此流的缓冲区,是为了保护硬盘

static void Main(string[] args) {             using (FileStream filewrite = new FileStream("file.txt", FileMode.Create, Fileaccess.Write))             {                 filewrite.WriteByte(101);                 filewrite.WriteByte(101);                 //清除此流的缓冲区                 filewrite.Flush();                 filewrite.WriteByte(101);                 filewrite.WriteByte(101);                 //每次写一个直接就会频繁的操作硬盘,             } }

 

(2)Seek(偏移,位置枚举)

static void Main(string[] args) {             using (FileStream fileRead = new FileStream("file.txt", FileMode.Open, FileAccess.Read))             {                 fileRead.Position = 4;                 fileRead.Seek(3, SeekOrigin.Current);                 int n = fileRead.ReadByte();                 Console.WriteLine((char)n);             }

 

其它流

(1)MemoryStream 内存流

            NetworkStream ns = new NetworkStream();

(2)NetworkStream 网络流

            MemoryStream ms = new MemoryStream();

读写流

(1) StreamReader

//读取文件流         static void Main(string[] args)         {             using(FileStream fileRead=new FileStream("成功.txt",FileMode.Open,FileAccess.Read))             {                 using (StreamReader sr = new StreamReader(fileRead,Encoding.Default))                 {                     //第一种读法                     //string str = sr.ReadLine();                     //while ((str = sr.ReadLine()) != null)                     //{                     //    Console.WriteLine(str);                     //}                     //第二种读法                     Console.WriteLine(sr.ReadToEnd());                 }             }         }

(2)StreamWriter

            static void Main(string[] args)

        {

            using (StreamWriter sw = new StreamWriter("成功.txt"))

            {

                sw.WriteLine();

            }

        }

 

File

(1) File关于文件读取的方法

            1)使用File文件读取流

                   //损失性能

            byte[] bs=File.ReadAllBytes("file.txt");

            2)管理文件

                   ->创建文件

                          File.Create("f:\\韩迎龙.txt", 10 * 1024 * 1024);

                   ->删除文件

                          File.Delete("f:\\韩迎龙.txt");

                   ->查文件

                          bool isExist = File.Exists("f:\\韩迎龙.txt");

                          Console.WriteLine(isExist);

(2)FileInfo

            1)创建文件

FileInfo file = new FileInfo("f:\\韩迎龙.txt"); //在内存中存在

            file.Create();  //创建文件

            2)设置属性

                   file.Attributes = FileAttributes.ReadOnly; //在属性中查看

(3)Copy方法 复制

            File.Copy("f:\\韩迎龙.txt", "f:\\111.txt");

(4)Move方法 移动

            File.Move("f:\\韩迎龙.txt", "f:\\1\\韩迎龙.txt");

(5)修改文件的全部名称

static void Main(string[] args)         {             string[] fnames = Directory.GetFiles("f:\\1");             //Array的Sort方法可以对数组进行排序             Array.Sort(fnames);             for (int i = 0; i < fnames.Length; i++)             {                 string temp = fnames[i];                 //获得文件名                 string fileName = Path.GetFileName(temp);                 //获得路径名                 string path = Path.GetDirectoryName(temp);                 //新的文件名                 string newPath = Path.Combine(path, i.ToString(new string('0', fnames.Length.ToString().Length)) + ".txt");                 File.Move(fnames[i], newPath);             }         }

Directory

(1) 增

            //创建文件夹

        Directory.CreateDirectory("F:\\2.exe");

(2)删

            Directory.Delete("f:\\1", true); //直接删除,在回收站中再找不到了

(3)和File的使用基本一样 

(4)得到文件夹下面的子文件夹

            string[] subDir=Directory.GetDirectories("文件夹路径");

(5)得到文件夹下面的所有子文件

 

string[] file= Directory.GetFiles("文件夹路径");   //先创建几个文件 string type = "mp3|mp4|doc|rmvb|txt|xls|exe|avi";   string path = @"F:\file"; if(!Directory.Exists(path)) {       Directory.Create(path); } Random rand=new Random(); string[] ts=type.Split('|'); for (int i = 0; i < 100; i++) {     File.Create(Path.Combine(path, Path.ChangeExtension(i.ToString(), ts[rand.Next(ts.Length)]))); }     string[] files = Directory.GetFiles(@"F:\file", "*.txt");