c#基于文件读写的操作

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();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值