File是.netI/O中比较重要的部分,所以我们来了解了解它。
我将从文件的创建、读、写、设置并获取文件信息或属性以及操作文件这五个方面来说。
1,创建
创建里面有这么几个方法。这里的创建不包括执行写的操作时候,发现文件不存在而创建文件这种情况,我们就说那总直接创建的。
用代码举例子
string path = @"C:UsersMYDocumentsVisual Studio 2015Projectstest2io";
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().GetBytes("this is a word");
fs.Write(info, 0, info.Length);
fs.Close();
fs.Dispose();
}
2,读
string path = @"C:UsersMYDocumentsVisual Studio 2015Projectstest2iotest1.txt"; //1,利用Filestream和StreamReader进行读取,open()和openread()对文件的类别无要求,opentext(),要求文件是UTF-8 编码文本文件 //关于FileStream如何读取,我们以后会讲,streamreader()在I/O(二)中已做示列 FileStream fs = File.Open(path,FileMode.Open); FileStream fs = File.OpenRead(path); StreamReader sr = File.OpenText(path); //2,直接读取出byte[],再转为你想要的,或是读出string[]、string byte[] bt = File.ReadAllBytes(path);//要打开的是一个二进制文件,将文件的内容读入一个字节数组,然后关闭该文件。 string str = File.ReadAllText(path);//打开一个文本文件,使用指定的编码读取文件中的所有文本,然后关闭此文件。 string[] lines = File.ReadAllLines(path,Encoding.UTF8);//打开一个文本文件,使用指定的编码读取文件的所有行,然后关闭该文件。 IEnumerableenumstr = File.ReadLines(path);//读取文件的行 //ReadLines和 ReadAllLines 的区别: //ReadLines:在返回整个字符串数组之前。你就可以访问它:用处:(1)linQ过滤掉一些你不想要的(2)将读取的值追加到别的文件 //ReadAllLines:必须等待整个字符串数组返回后,才能访问数组。 因此,在读大的文件时,ReadLines 可能会更有效。
3,写
string path = @"C:UsersMYDocumentsVisual Studio 2015Projectstest2iotest1.txt";
string encodestr = "hello world";
byte[] bytes = new UTF8Encoding().GetBytes(encodestr);
string[] lines = { "你好", "hhhh" };
//创建新文件并写入指定的字节数组,然后关闭该文件。 文件存在则覆盖。
File.WriteAllBytes(path, bytes);
//创建新文件并写入一个或多个字符串,然后关闭该文件。 文件存在则覆盖。每个元素都是一行。
File.WriteAllLines(path, lines);
//创建新文件并写入内容,然后关闭文件。 文件存在则覆盖。
File.WriteAllText(path, "嘻嘻嘻");
//向一个文件中追加行,然后关闭该文件。文件存在不覆盖,只追加,不存在报错
File.AppendAllLines(path, File.ReadAllLines(path, Encoding.UTF8));
//将指定的字符串追加到文件中,如果文件还不存在则创建该文件。
File.AppendAllText(path, "再来一次");
//通过StreamWriter写入
StreamWriter sw = File.AppendText(path);
sw.Write("再来二次");
sw.Close();
sw.Dispose();
4,获取/设置文件信息或属性
string path = @"C:UsersMYDocumentsVisual Studio 2015Projectstest2iotest1.txt"; DateTime dt = DateTime.Now; File.SetCreationTime(path, dt); Console.WriteLine(File.GetCreationTime(path)); File.SetCreationTimeUtc(path, dt); Console.WriteLine(File.GetCreationTimeUtc(path)); //上次访问时间 File.SetLastAccessTime(path, dt); Console.WriteLine(File.GetLastAccessTime(path)); File.SetLastAccessTimeUtc(path, dt); Console.WriteLine(File.GetLastAccessTimeUtc(path)); //上次写入时间 File.SetLastWriteTime(path, dt); Console.WriteLine(File.GetLastWriteTime(path)); File.SetLastWriteTimeUtc(path, dt); Console.WriteLine(File.GetLastWriteTimeUtc(path)); ///设置/获取文件的属性(https://docs.microsoft.com/zh-cn/dotnet/api/system.io.fileattributes?view=netcore-3.1) //File.SetAttributes(path,FileAttributes.ReadOnly); FileAttributes fa = File.GetAttributes(path); Console.WriteLine(fa.ToString()); Console.ReadLine();5,文件操作(比较常用)
以下比较常用的吧,我列举了几个
File.Exists(文件本地路径);
File.Copy(源文件本地路径,目的地文件路径);
File.Move(源文件本地路径,目的地文件路径);
File.Encrypt(文件本地路径);
File.Decrypt(文件本地路径);
File.Delete(文件本地路径);
以上就是File类大致的一些用法,总结一下无非就是,要不直接操作文件,要不就是利用流进行读写。
具体使用得结合实际项目来。自己平时操作一下,做起项目就能得心应手。还需多练习啊!!



