C#编程入门_文件处理IO_17

来源:互联网 发布:l28t手环软件 编辑:程序博客网 时间:2024/06/12 18:13

21篇C#博客的配套源码


Path类

在程序中经常会对文件的路径进行操作,例如获取文件的扩展名,获取文件或文件夹的路径等,为了实现此类功能,C#中提供了Path类。Path类中包含了一系列用于对文件路径进行操作的方法
这里写图片描述

参考代码

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Path类{    class Program    {        static void Main(string[] args)        {            string path = "D:\\FileTest\\Test.txt";            //绝对路径            string path1 = @"D:\FileTest\Test.txt";            //拼接成一个完整的路径            string path2 = Path.Combine(@"D:\FileTest", @"Test.txt");            string path3 = @"D:\FileTest\Test\xxx\01\12.txt";            Console.WriteLine(path2);            //得到12.txt所在的文件夹的路径            Console.WriteLine(Path.GetDirectoryName(path3));            //得到文件的扩展名            Console.WriteLine(Path.GetExtension(path3));            //得到文件名  包含扩展名            Console.WriteLine(Path.GetFileName(path3));            //得到文件名  但是不包含扩展名            Console.WriteLine(Path.GetFileNameWithoutExtension(path3));            //得到当前文件所在的绝对路径            Console.WriteLine(Path.GetFullPath("12.txt"));            string path4 = Path.GetFullPath("12.txt");            Console.WriteLine(Path.GetPathRoot(path4));            Console.WriteLine(Path.GetRandomFileName());        }    }}

File类

File 类是一个静态类,它提供了许多静态方法,用于处理文件,使用这些方法可以对文件进行创建、移动、查询和删除等操作,接下来介绍 File 类的一些常用的静态方法,如表所示。

这里写图片描述
参考代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace File类{    class Program    {        static void Main(string[] args)        {            string path = @"D:\FileTest\1707A\aa.txt";            string destPath = @"D:\FileTest\1707A\aa.txt";            //Test01(path);            //Test01("王垚.txt");            //Test02(path, destPath);            //Test03(path);            Test04(path);        }        //文件创建        static void Test01(string path)        {            //文件的创建            if (File.Exists(path))            {                Console.WriteLine("文件已经存在");            }            else            {                //如果不存在则创建文件                File.Create(path);            }         }        //文件的移动  和文件的重命名        static void Test02(string sourcePath,string destPath)        {            File.Move(sourcePath, destPath);        }        //删除文件        static void Test03(string path)        {            File.Delete(path);        }        static void Test04(string path)        {            //从文件读取数据的流            FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read);            byte[] byteArr = new byte[4096];            int len = 0;            string destPath = @"D:\FileTest\1707A\Newaa.txt";            //写入文件的流            FileStream fsWrite = File.Open(destPath, FileMode.OpenOrCreate, FileAccess.Write);            //len表示 在调用Read方法的时候  从文件中实际读取的字节数            while ((len = fs.Read(byteArr, 0, byteArr.Length)) != 0)            {                fsWrite.Write(byteArr, 0, len);            }        }    }}

Directory类

Directory类是静态类,提供了许多静态方法用于对目录进行操作,例如创建、删除、查询和移动目录等。
这里写图片描述
Directory类和File类中的代码雷同 由于篇幅 代码不在赘述


FileInfo和DirectoryInfo类

FileInfo、DirectoryInfo类和File类、Directory类的使用功能差不多,只不过FileInfo和FDirectoryInfo类都是实例类,而后者是静态类,除了他们的独特的属性外,其他的内容都差不多,代码不在赘述
FileInfo类
FileInfo类与File类有些类似,它们都可以对磁盘上的文件进行操作。不同的是FileInfo类是实例类,所有的方法都只能在实例化对象后才能调用。创建 FileInfo 类对象时必须传递一个文件路径作为参数,具体语法格式如下。
FileInfo aFile = new FileInfo(@”C:\Data.txt”);
上述代码表示使用 FileInfo 类创建一个对象,将文件路径作为参数,而路径中“@”符号表示不解析转义字符,如果没有“@”前缀就需要用“\”替代“\”。通过前面的学习可知,“\”是一个转义字符,在程序中要表示一个“\”就需要使用“\”。例如下面这句代码。
FileInfo aFile = new FileInfo(“C:\Data.txt”);

FileInfo类除了有许多与File类相似的方法外,同时也有它特有的属性
这里写图片描述
DirectoryInfo类
DirectoryInfo类的功能与Directory类相似,不同的是DirectoryInfo是一个实例类,该类不仅拥有与Directory类功能相似的方法,而且还具有一些特有的属性。下面图中是常用的属性
这里写图片描述


FileStream类

FileStream 类表示在磁盘或网络路径上指向文件的流,并提供了在文件中读写字节和字节数组的方法,通过这些方法,FileStream 对象可以读取诸如图像、声音、视频、文本文件等,也就是说FileStream类能够处理各种数据文件。
FileStream类有很多重载的构造方法,其中最常用的是带有三个参数的构造方法,具体如下。
FileStream(string path, FileMode mode, FileAccess access);
上述构造方法中,第一个参数path表示的是文件路径名,第二个参数mode表示如何打开或创建文件,第三个参数access用于确定 FileStream 对象访问文件的方式。除了这个构造方法外,FileStream类还有一些常用的方法
这里写图片描述

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace FileStream类{    class Program    {        static void Main(string[] args)        {            string path1 = @"D:\FileTest\1707A\aa.txt";            string path2 = @"D:\FileTest\1707A\cc.txt";            Test01(path1,path2);        }        static void Test01(string sourcePath,string destPath)        {            FileStream fsRead = null;            FileStream fsWrite = null;            try            {                fsRead = new FileStream(sourcePath, FileMode.OpenOrCreate, FileAccess.Read);                fsWrite = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write);                int temp = -1;                while ((temp = fsRead.ReadByte()) != -1)                {                    fsWrite.WriteByte((byte)temp);                }            }            catch (IOException e)            {                Console.WriteLine(e.Message);            }            catch(Exception e)            {                Console.WriteLine(e.Message);            }            finally            {                fsRead.Close();                fsWrite.Close();            }        }    }}

BufferedStream类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace BufferedStream类{    class Program    {        static void Main(string[] args)        {            string path = @"D:\FileTest\1707A\Newaa.txt";            string path1 = @"D:\FileTest\1707A\Newcc.txt";            FileStream fsRead = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read);            BufferedStream bsRead = new BufferedStream(fsRead);            FileStream fsWrite = new FileStream(path1,FileMode.OpenOrCreate,FileAccess.Write);            BufferedStream bsWrite = new BufferedStream(fsWrite);            int len = 0;            byte[] byteArr = new byte[4096];            while ((len = bsRead.Read(byteArr,0,byteArr.Length))!=0)            {                bsWrite.Write(byteArr,0,len);            }            bsWrite.Flush();            bsWrite.Close();        }    }}

StramReader和StreamWriter类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace StreamReaderStreamWriter{    class Program    {        static void Main(string[] args)        {            string path1 = @"D:\FileTest\1707A\Newaa.txt";            string path2 = @"D:\FileTest\1707A\Newbb.txt";            Test02(path1,path2);        }        static void Test01(string path1,string path2)        {            FileStream fsReader = new FileStream(path1,FileMode.OpenOrCreate,FileAccess.Read);            StreamReader sr = new StreamReader(fsReader,Encoding.Default);            FileStream fsWriter = new FileStream(path2,FileMode.OpenOrCreate,FileAccess.Write);            StreamWriter sw = new StreamWriter(fsWriter,Encoding.Unicode);            int temp = -1;            while((temp = sr.Read()) != -1)            {                sw.Write((char)temp);            }            //刷出缓存区的数据            sw.Flush();            //关闭流            sw.Close();            //关闭流            fsWriter.Close();        }        static void Test02(string path1, string path2)        {            FileStream fsReader = new FileStream(path1,FileMode.OpenOrCreate,FileAccess.Read);            StreamReader sr = new StreamReader(fsReader,Encoding.Default);            FileStream fsWriter = new FileStream(path2,FileMode.OpenOrCreate,FileAccess.Write);            StreamWriter sw = new StreamWriter(fsWriter,Encoding.Default);            string str = string.Empty;            while((str = sr.ReadLine()) != null)            {                sw.WriteLine(str);            }            sw.Flush();            sw.Close();        }    }}