一、路径
路径可以分为两种,分别为绝对路径和相对路径。
绝对路径:包含完整的文件或文件夹位置信息,从文件的根目录开始的完整路径。例如:C:\Users\Username\Documents\example.txt
相对路径:是从当前工作目录开始的路径,描述了从当前目录到目标文件或目录的路径例如,如果当前工作目录是C:\Users\Username,那么相对路径Documents\example.txt表示C:\Users\Username\Documents\example.txt。
二、读取文件中内容的操作
读取文件中内容的操作可分为以下几步:
1. 首先创建一个文本文件路径地址
2. 使用数据读取流 StreamReader:数据读取流,提供读取的功能,需要传入读取的文件路径
3. 使用读取流中的函数,ReadToEnd() ,将文本文件中所有的内容用string类型接收
4. 在控制台输出接收的string类型
5. 关闭数据读取流操作,关闭文件操作,注意,使用完毕一定要关闭
string namePath = @"C:\Users\95287\OneDrive\桌面\1.txt"; //1.文本文件路径地址
StreamReader sr = new StreamReader(namePath); //2.StreamReader:数据读取流,提供读取的功能,需要传入读取的文件路径
string content = sr.ReadToEnd(); //3.将此文本文件中的所有内容赋值给content
Console.WriteLine(content); //4.输出所有内容
sr.Close(); //5.关闭文件读取流操作
三、写入文件中内容的操作
写入文件中内容的操作可以分为以下几步:
1. 首先创建一个文本文件路径地址
2. 使用数据写入流 StreamWriter:数据写入流,提供写入的功能,需要传入写入的文件路径
3. 使用 数据写入流的变量类型.WriteLine(); 将想要写入的内容写入文本文件中
4. 关闭数据写入流操作,关闭文件操作,注意,使用完毕一定要关闭
string namePath = @"C:\Users\95287\OneDrive\桌面\1.txt"; //1.文本文件路径地址
StreamWriter sw = new StreamWriter(namePath); //2.StreamWriter:数据写入流,提供写入文本文件中的操作,需要传入读取的文件路径
sw.WriteLine("Alice"); //3.单独写入一段数据
sw.Write("Bob"); //写入一段数据不自动换行
//如果是批量的数据也可以通过List配合foreach循环来快速写入
List<int> ints = new List<int>(){1,2,3,4,5,6,7,8,9};
foreach(int i in ints)
{
sw.Write(i + ",");
}
sw.Close(); //4.关闭文件
四、检测文件是否存在
使用文件操作的时候,需要首先检测是否文件是否存在
string FilePath = @"C:\Users\95287\OneDrive\桌面\name.text";
//File负责提供文件操作所需的所有方法
//Exists检测当前路径下是否包含有文件
//根据路径检测文件是否存在,存在则返回true
if(!File.Exists(FilePath))
{
Console.WriteLine("无文件,创建文件");
//Stream:流,一种操作文件数据的概念
//1.代表文件的位置
//2.文件的创建方式,枚举类型
//3.文件的操作权限
FileStream fs = new FileStream(FilePath,FileMode.Create,FileAccess.Write);
fs.Close;
}
else
{
Console.WriteLine("有文件,追加或者读取");
//OpenOrCreate 和 ReadWrite 配合来用
FileStream fs = new FileStream(FilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
StreamReader streamReader = new StreamReader(fs);
string data = streamReader.ReadToEnd();
Console.WriteLine(data);
streamReader.Close();
fs.Close();
FileStream fs1 = new FileStream(FilePath, FileMode.Append, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fs1);
streamWriter.WriteLine("下雪了,very beautiful!!!");
streamWriter.Close();
fs1.Close();
}
五、有关文件的一些API
File提供了静态的增删改查的方法
1.写入文件的方法
string filePath = @"C:\Users\95287\OneDrive\桌面\name.txt";
string content = "主要讲解文件的方法";
File.WriteAllText(filePath, content);//写入文件
2.读取文件的操作
string filePath = @"C:\Users\95287\OneDrive\桌面\name.txt";
string content = "主要讲解文件的方法";
if (File.Exists(filePath)) //文件的读取首先要判断文件是否存在
{
//读取
string dataStr = File.ReadAllText(filePath);
Console.WriteLine(dataStr);
}
//批量写入
string[] contentArr = { "1", "2", "3", "4", "5" };
File.WriteAllLines(filePath, contentArr);
3.文件的移动
if (File.Exists(filePath))
{
string destFile = @"C:\Users\95287\OneDrive\桌面\name.txt";
//Move移动,如果文件位置不存在,则报错
//移动之后会用新名字替代原来的文件
File.Move(filePath, destFile);
}
4.文件的删除
File.Delete(filePath);
六、文件夹是否存在
string dicPath = @"C:\Users\95287\OneDrive\桌面\name";//文件夹路径
string dataPath = @"data.txt";//文件
//Directory.Exists:当前路径下是否包含该文件夹
if (!Directory.Exists(dicPath))
{
//不包含,创建
Directory.CreateDirectory(dicPath);
}
else
{
//文件夹存在则添加内容
//Combine:拼接两个路径为一个
string filePath2 = Path.Combine(dicPath, dataPath);
Console.WriteLine(filePath2);
File.WriteAllText(filePath2, "哈哈哈1");
}