namespace 文件的基本操作
{
class Program
{
static void Main(string[] args)
{
//1.文件的创建
string path = "abc.txt";
if(!File.Exists(path))
{
using (File.CreateText(path))
{
}
}
// 2.1 File.AppendText
//using (StreamWriter sw = File.AppendText(path))
//{
// sw.WriteLine("中国湖北是我的家!");
//}
//2.2 File.AppendAllText
// File.AppendAllText(path,"fdsafdsafsa,china");
//2.3 File.AppendAllLines
//File.AppendAllLines(path, new string[] { "china", "山海关","黯然销魂掌" });
//2.4 File.Open
//using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write))
//{
// byte[] buffers = Encoding.UTF8.GetBytes("飞流直下三千尺,疑是银河落九天"+"\r\n");
// fs.Write(buffers, 0, buffers.Length);
//}
//2.5 File.OpenWrite
//using (FileStream fs = File.OpenWrite(path))
//{
// byte[] buffers = Encoding.UTF8.GetBytes("飞流直下三千尺,疑是银河落九天" + "\r\n");
// fs.Write(buffers, 0, buffers.Length);
//}
//2.6 WriteAllBytes
// File.WriteAllBytes(path,Encoding.UTF8.GetBytes("云想衣裳花想容,春风扶老厉害了" + "\r\n"));
// File.WriteAllLines(path,new string[]{ "123","china"});
// File.WriteAllText(path,"天上的星星不说话");
//3.1 File.OpenText
//using (StreamReader sr = File.OpenText(path))
//{
// Console.WriteLine(sr.ReadToEnd());
//}
//3.2 =File.OpenRead
//using (FileStream fs =File.OpenRead(path))
//{
// byte[] buffer = new byte[1024];
// int t= fs.Read(buffer,0,1024);
// Console.WriteLine(Encoding.UTF8.GetString(buffer,0,t));
//}
//3.3 File.Open
//using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read))
//{
// byte[] buffer = new byte[1024];
// int t= fs.Read(buffer,0,1024);
// string s = Encoding.UTF8.GetString(buffer,0,t);
// Console.WriteLine(s);
//}
//3.4 File.ReadAllBytes
//byte[] buffer= File.ReadAllBytes(path);
//string s = Encoding.UTF8.GetString(buffer,0,buffer.Length);
//Console.WriteLine(s);
//3.5 File.ReadAllLines
// string [] strs= File.ReadAllLines(path);
//for (int i = 0; i < strs.Length; i++)
//{
// Console.Write(strs[i]+"\r\n");
//}
//3.6 File.ReadAllText
//string s= File.ReadAllText(path);
//Console.WriteLine(s);
//3.7 File.ReadLines
IEnumerable<string> s= File.ReadLines(path);
foreach (string item in s)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}