C# 四十八、文件操作与文件流 一

C#中操作文件的流模型——文件和流

文件: 

  • 从广义上可看成是保存在磁盘上的二进制字节,是按住一定格式存储的信息,因此能用流对文件进行操作,如读取文件内容、将信息写入文件等;

流:

  • C#中流的概念可以和生活中的“流”相对应,有源头(文件),传输管道(数据流入/流出),目的地/流向;
  • C#中流是面向对象的抽象概念,是二进制字节序列;
  • 在C#中通过.NET的System.IO模型以流的方式对各种数据文件进行访问,按照流的方向把流分为两种:输入流和输出流;
    • 输入流用于将数据读到程序可以访问的内存或变量中。输入流可以来自任何源,对于磁盘文件,可以理解为以文件为源,以内存为目的地。
    • 输出流用于向某些外部目标写入数据,可以是磁盘文件、打印设备或另一个程序。在此主要关注以内存为源、以文件为目的地的输出流。
  • 流涉及三个基本操作:
    • 从流读取:读取是从流到数据结构(如字节数组)的数据传输
    • 向流写入:写入是从数据结构到流的数据传输
    • 支持查找:查找是对流内的当前位置进行查询和修改

区别:

  • 文件是存储在存储介质上的数据集,是静态的,它具有名称和相应的路径;
  • 当打开一个文件并对其进行读/写时,该文件就成为流(Stream);
  • 流不仅仅是指打开的磁盘文件,还可以是网络数据、控制台应用程序中的键盘输入和文本显示,甚至是内存缓存区的数据读/写。因此,流是动态的,它代表正处于输入/输出状态的数据,是一种特殊的数据结构。

System.IO模型:

  • System.IO模型提供了一个面向对象的方法来访问文件系统,提供了很多针对文件、文件夹的操作功能,特别是以流(Stream)的方式对各种数据进行访问,这种访问方式不但灵活而且可以保证编程接口的统一。
  • System.IO模型实现包含在System.IO命名空间中,该命名空间包含允许在数据流和文件上进行同步和异步读取及写入、提供基本文件和文件夹操作的各种类,即System.IO模型是一个文件操作类库,包含的类可用于文件的创建、读/写、复制、移动和删除等操作。

System.IO模型中,借助文件流进行文件操作的常用步骤:

1、用File类打开操作系统文件

2、建立对应的文件流,即FileStream对象

3、用StreamReader/StreamWriter类提供的方法对文件流(文本文件)进行读/写或用BinaryReader/BinaryWriter类提供的方法对文件流(二进制文件)进行读/写。

System.IO

 System.IO命名空间,包含用于在文件中读写数据的类,只有在C#应用程序中引用此名称空间才能访问这些类,而不必完全限定类型名。

用于访问文件系统的类

File静态实用类,提供许多静态方法,用于创建、移动、复制、打开和删除文件,并协助创建Filestream对象
Directory静态实用类,提供许多静态方法,公开用于创建、移动、复制和删除目录,枚举通过目录和子目录。无法继承此类
FileInfo表示磁盘上的物理文件,该类包含处理此文件的方法。要完成对文件的读写工作,就必须创建Stream对象。提供创建、复制、删除、移动和打开文件的实例方法,并帮助创建FileStream对象。无法继承此类
DirectoryInfo表示磁盘上的物理目录,该类保含处理此目录的方法。公开用于创建、移动、枚举目录和子目录的实例方法。无法继承此类
Path实用类,用于处理路径名称
FileStream公开以文件为主的Stream,既支持同步读/写操作,也支持异步读/写操作
MemoryStream创建其支持存储区为内存的流
Stream

提供字节序列的一般视图

Stream类是所有流类的抽象基类,它提供了流的基本功能

StreamReader实现一个TextReader,使其以一种特定的编码从字节流中读取字符
StreamWriter实现一个TextWriter,使其以一种特定的编码向流中写入字符
StringReader实现从字符串进行读取的TextReader
StringWriter实现一个用于将信息写入字符串的TextWriter。该信息存储在基础StringBuilder中
TextReader表示可读取连续字符系列的读取器
TextWriter表示可以编写一个有序字符系列的编写器。该类为抽象
BinaryReader用特定的编码将基元数据类型读作二进制值
FileSystemInfo用作FileInfo和DirectoryInfo的基类,可以使用多态性同时处理文件和目录
FileSystemWatcher用于监控文件和目录,提供了这些文件和目录发生变化时应用程序可以捕获的事件
DriveInfo提供对有关驱动器的信息的访问

System.IO命名空间的类大致可分为以下三组:

1、操作流的类:包括文件流、内存流、以及读/写这些流的类;

2、操作目录的类:包括对文件夹目录进行创建、移动、删除等操作,以及对磁盘信息进行访问的类;

3、操作文件的类:包括对文件进行创建、移动、删除等操作,以及获取文件信息等。

File类

部分常用方法 

 public static FileStream Create(string path);

官方摘要:在指定路径中创建或覆盖文件。

参数说明:

  • path:要创建的文件的路径及名称。

返回结果:一个 System.IO.FileStream,它提供对 path 中指定的文件的读/写访问。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            File.Create(@"E:\Test\abc.txt");
            File.Create(@"E:\Test\abc.doc");
        }
    }
}

public static void Copy(string sourceFileName, string destFileName);

官方摘要:将现有文件复制到新文件。 不允许覆盖同名的文件。

参数说明:

  • sourceFileName:要复制的文件。
  • destFileName:目标文件的名称。 它不能是一个目录或现有文件。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            //File.Create(@"E:\Test\abc.txt");

            File.Copy(@"E:\Test\abc.txt", @"E:\Test\abcd.txt");

        }
    }
}

public static void WriteAllText(string path, string contents);

官方摘要:创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则覆盖该文件。

参数说明:

  • path:要写入的文件。
  • contents:要写入文件的字符串。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            //File.Create(@"E:\Test\abc.txt");

            File.WriteAllText(@"E:\Test\abc.txt","石家庄");

        }
    }
}

public static void Delete(string path);

官方摘要:删除指定的文件。

参数说明:

  • path:要删除的文件的名称。 该指令不支持通配符。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            //File.Create(@"E:\Test\abc.txt");

            File.Delete(@"E:\Test\abc.txt");

        }
    }
}

public static bool Exists(string path);

官方摘要:确定指定的文件是否存在。

参数说明:

  • path:要检查的文件。

返回结果:如果调用方具有要求的权限并且 true 包含现有文件的名称,则为 path;否则为 false。 如果path为null (一个无效路径或零长度字符串),则此方法也将返回false。 如果调用方不具有读取指定文件所需的足够权限,则不引发异常并且该方法返回 false,这与 path 是否存在无关。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            File.Create(@"E:\Test\abc.txt");

            Console.WriteLine(File.Exists(@"E:\Test\abc.txt"));

            Console.ReadKey();
        }
    }
}

--->
True

public static void AppendAllText(string path, string contents);

官方摘要:打开一个文件,向其中追加指定的字符串,然后关闭该文件。 如果文件不存在,此方法将创建一个文件,将指定的字符串写入文件,然后关闭该文件。

参数说明:

  • path:要将指定的字符串追加到的文件。
  • contents:要追加到文件中的字符串。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            //File.Create(@"E:\Test\abc.txt");

            //File.WriteAllText(@"E:\Test\abc.txt","石家庄");

            File.AppendAllText(@"E:\Test\abc.txt","新华区");

        }
    }
}

public static DateTime GetCreationTime(string path);

官方摘要:返回指定文件或目录的创建日期和时间。

参数说明:

  • path:要获取其创建日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为指定文件或目录的创建日期和时间。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            File.Create(path);
            Console.WriteLine(File.GetCreationTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 14:51:13

public static DateTime GetCreationTimeUtc(string path);

官方摘要:返回指定的文件或目录的创建日期及时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要获取其创建日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为指定文件或目录的创建日期和时间。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            File.Create(path);
            Console.WriteLine(File.GetCreationTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 6:51:13

 public static DateTime GetLastAccessTime(string path);

官方摘要:返回上次访问指定文件或目录的日期和时间。

参数说明:

  • path:要获取其访问日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为上次访问指定文件或目录的日期和时间。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            //File.Create(path);
            Console.WriteLine(File.GetLastAccessTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 14:54:43

public static DateTime GetLastAccessTimeUtc(string path);

官方摘要:返回上次访问指定的文件或目录的日期及时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要获取其访问日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为上次访问指定文件或目录的日期和时间。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            //File.Create(path);
            Console.WriteLine(File.GetLastAccessTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 6:54:43

public static DateTime GetLastWriteTime(string path);

官方摘要:返回上次写入指定文件或目录的日期和时间。

参数说明:

  • path:要获取其写入日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为上次写入指定文件或目录的日期和时间。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            Console.WriteLine(File.GetLastWriteTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 15:02:14

public static DateTime GetLastWriteTimeUtc(string path);

官方摘要:返回上次写入指定的文件或目录的日期和时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要获取其写入日期和时间信息的文件或目录。

返回结果:一个 System.DateTime 结构,它被设置为上次写入指定文件或目录的日期和时间。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            Console.WriteLine(File.GetLastWriteTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 7:02:14

public static void Move(string sourceFileName, string destFileName);

官方摘要:将指定文件移到新位置,提供要指定新文件名的选项。

参数说明:

  • sourceFileName:要移动的文件的名称。 可以包括相对或绝对路径。
  • destFileName:文件的新路径和名称。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string pathA = @"E:\TestA\abc.txt";
            string pathB = @"E:\TestB\abc.txt";
            File.Move(pathA, pathB);

            Console.ReadKey();
        }
    }
}

public static string ReadAllText(string path);

官方摘要:打开一个文本文件,读取文件的所有行,然后关闭该文件。

参数说明:

  • path:要打开以进行读取的文件。

返回结果:包含文件所有行的字符串。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            Console.WriteLine(File.ReadAllText(path));

            Console.ReadKey();
        }
    }
}

--->
aaa
bbb
ccc

public static string[] ReadAllLines(string path);

官方摘要:打开一个文本文件,读取文件的所有行,然后关闭该文件。

参数说明:

  • path:要打开以进行读取的文件。

返回结果:包含文件所有行的字符串数组。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            foreach (var item in File.ReadAllLines(path))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

--->
aaa
bbb
ccc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            string[]a = File.ReadAllLines(path);
            Console.WriteLine(a[1]);

            Console.ReadKey();
        }
    }
}

--->
bbb

public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName);

官方摘要:使用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。

参数说明:

  • sourceFileName:替换由 destinationFileName 指定的文件的文件名。
  • destinationFileName:被替换文件的名称。
  • destinationBackupFileName:备份文件的名称。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string pathA = @"E:\Test\abc.txt";
            string pathB = @"E:\Test\abcd.txt";
            string pathC = @"E:\Test\abcde.txt";
            File.Replace(pathA, pathB, pathC);

            Console.ReadKey();
        }
    }
}

public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);

官方摘要:用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份,还可以忽略合并错误。

参数说明:

  • sourceFileName:替换由 destinationFileName 指定的文件的文件名。
  • destinationFileName:被替换文件的名称。
  • destinationBackupFileName:备份文件的名称。
  • ignoreMetadataErrors:如果忽略从被替换文件到替换文件的合并错误(如特性和访问控制列表 (ACL)),则为 true,否则为 false。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string pathA = @"E:\Test\abc.txt";
            string pathB = @"E:\Test\abcd.txt";
            string pathC = @"E:\Test\abcde.txt";
            File.Replace(pathA, pathB, pathC,true);

            Console.ReadKey();
        }
    }
}

public static DateTime SetCreationTime(string path, DateTime creationTime);

官方摘要:设置创建该文件的日期和时间。

参数说明:

  • path:要设置其创建日期和时间信息的文件。
  • creationTime:一个 System.DateTime,它包含要为 path 的创建日期和时间设置的值。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetCreationTime(path, new DateTime(2018,12,23,15,37,30));

            Console.WriteLine(File.GetCreationTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 15:37:30

public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc);

官方摘要:设置文件创建的日期和时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要设置其创建日期和时间信息的文件。
  • creationTimeUtc:一个 System.DateTime,它包含要为 path 的创建日期和时间设置的值。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetCreationTimeUtc(path, new DateTime(2018,12,23,7,37,30));

            Console.WriteLine(File.GetCreationTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 7:37:30

public static void SetLastAccessTime(string path, DateTime lastAccessTime);

官方摘要:设置上次访问指定文件的日期和时间。

参数说明:

  • path:要设置其访问日期和时间信息的文件。
  • lastAccessTime:一个 System.DateTime,它包含要为 path 的上次访问日期和时间设置的值。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetLastAccessTime(path, new DateTime(2018,12,23));

            Console.WriteLine(File.GetLastAccessTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 0:00:00

public static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc);

官方摘要:设置上次访问指定的文件的日期和时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要设置其访问日期和时间信息的文件。
  • lastAccessTime:一个 System.DateTime,它包含要为 path 的上次访问日期和时间设置的值。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetLastAccessTimeUtc(path, new DateTime(2018,12,23));

            Console.WriteLine(File.GetLastAccessTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 0:00:00

public static void SetLastWriteTime(string path, DateTime lastWriteTime);

官方摘要:设置上次写入指定文件的日期和时间。

参数说明:

  • path:要设置其日期和时间信息的文件。
  • lastWriteTime:一个 System.DateTime,它包含要为 path 的上次写入日期和时间设置的值。 该值用本地时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetLastWriteTime(path, new DateTime(2018,12,23));

            Console.WriteLine(File.GetLastWriteTime(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 0:00:00

public static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc);

官方摘要:设置上次写入指定的文件的日期和时间,其格式为协调通用时 (UTC)。

参数说明:

  • path:要设置其日期和时间信息的文件。
  • lastWriteTimeUtc:一个 System.DateTime,它包含要为 path 的上次写入日期和时间设置的值。 该值用 UTC 时间表示。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";

            File.SetLastWriteTimeUtc(path, new DateTime(2018,12,23));

            Console.WriteLine(File.GetLastWriteTimeUtc(path));

            Console.ReadKey();
        }
    }
}

--->
2018/12/23 0:00:00

public static void WriteAllLines(string path, string[] contents);

官方摘要:创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。

参数说明:

  • path:要写入的文件。
  • contents:要写入文件的字符串数组。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test_CSharp
{    
    class Program
    {       
        static void Main(string[] args)
        {
            string path = @"E:\Test\abc.txt";
            string[] s = { "啊啊啊", "啵啵啵", "呲呲呲" };

            File.WriteAllLines(path, s);

            Console.ReadKey();
        }
    }
}

System.IO.Compression

System.IO.Compression命名空间,允许读写压缩文件。

DeflateStream表示在写入时自动压缩数据或在读取时自动解压缩的流,使用Deflate算法来实现压缩
GZipStream表示在写入时自动压缩数据或在读取时自动解压缩的流,使用GZIP(GNUZip)算法来实现压缩
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值