见过不少人、经过不少事、也吃过不少苦,感悟世事无常、人心多变,靠着回忆将往事串珠成链,聊聊感情、谈谈发展,我慢慢写、你一点一点看......
1.创建文件
string
filePath = ``@"c:\myFile.txt"``;
FileStream fileStream = File.Create(filePath);
fileStream.Close();
2.文件写入
string
content = ``"写入内容"``;
File.WriteAllText(filePath, content, Encoding.UTF8);
3.文件读取(读取成字符串)
string
content = File.ReadAllText(filePath,Encoding.UTF8);
4.文件读取(读取成数组)
string[] content = File.ReadAllLines(filePath, Encoding.UTF8);
for
(``int
i = 0; i < content.Length; i++)
{
``Console.WriteLine(content[i]);
}
5.采用流(Stream)的方式来读取内容
//初始化FileStream
FileStream fs = ``new
FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr4 = ``new
StreamReader(fs, Encoding.UTF8);
string
line = ``string``.Empty;
while``((line = sr4.ReadLine()) != ``null``)
{
``Console.WriteLine(line);
}
sr4.Close();
6.采取流方式一次读完
StreamReader sr = ``new
StreamReader(filePath, Encoding.UTF8);
string
content = sr.ReadToEnd();
Console.WriteLine(content);
sr.Close();
7.文件复制
string
sourceFilePath = ``@"c:\myFile.txt"``;
string
destinationFilePath = ``@"d:\myFile_copy.txt"``;
File.Copy(sourceFilePath, destinationFilePath);
8.文件移动
string
sourceFilePath = ``@"c:\myFile.txt"``;
string
destinationFilePath = ``@"d:\myFile.txt"``;
File.Move(sourceFilePath, destinationFilePath);
9.文件重命名
string
filePath = ``@"c:\myFile.txt"``;
string
newFilePath = ``@"d:\myFile_new.txt"``;
File.Move(filePath, newFilePath);
10.文件删除
string
newFilePath = ``@"d:\myFile_new.txt"``;
File.Delete(newFilePath);
关注我,不失联。有啥问题请留言。