File是.netI/O中比较重要的部分,所以我们来了解了解它。
我将从文件的创建、读、写、设置并获取文件信息或属性以及操作文件这五个方面来说。
1,创建
创建里面有这么几个方法。这里的创建不包括执行写的操作时候,发现文件不存在而创建文件这种情况,我们就说那总直接创建的。
用代码举例子
string path = @"C:\Users\MY\Documents\Visual Studio 2015\Projects\test2\io\";
if(!File.Exists(path + "test1.txt"))
{
//1、复制,如果源文件(第一个参数)不存在则报错,
//第三个参数:如果目的地文件(第二个参数)存在,覆不覆盖,true:覆盖,false:不覆盖,且报错--->文件已存在
File.Copy(path + "text.txt", path + "test1.txt", true);
//2、创建或打开用于写入 UTF-8 编码文本的文件。 如果该文件已存在,将覆盖其内容
StreamWriter sw = File.CreateText(path + "test1.txt");
sw.WriteLine("hello world");
sw.Close();
sw.Dispose();
//3、直接创建文件(已存在则直接覆盖)
FileStream fs = File.Create(path + "test1.txt", 20, FileOptions.Asynchronous);
byte[] info = new UTF8Encoding