概述
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("飞流直下三千尺,疑是银河落九天"+"rn");
// fs.Write(buffers, 0, buffers.Length);
//}
//2.5 File.OpenWrite
//using (FileStream fs = File.OpenWrite(path))
//{
// byte[] buffers = Encoding.UTF8.GetBytes("飞流直下三千尺,疑是银河落九天" + "rn");
// fs.Write(buffers, 0, buffers.Length);
//}
//2.6 WriteAllBytes
// File.WriteAllBytes(path,Encoding.UTF8.GetBytes("云想衣裳花想容,春风扶老厉害了" + "rn"));
// 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]+"rn");
//}
//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();
}
}
}
最后
以上就是懦弱花生为你收集整理的c#基于文件读写的操作的全部内容,希望文章能够帮你解决c#基于文件读写的操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复