zoukankan      html  css  js  c++  java
  • 黑马程序员——File笔记读,写,复制

     #region ReadAllBytes

    byte[] buffer = File.ReadAllBytes(@"C:UsersdellDesktop新建文件夹.txt");

    //将字节数组转换成字符串
    //解决乱码 GB2312 GBK Default UTF8
    //string str = System.Text.Encoding.UTF8.GetString(buffer);
    //Console.WriteLine(str);
    //Console.ReadKey();
    #endregion


    #region ReadAllLines
    //string[] str = File.ReadAllLines(@"C:UsersdellDesktop新建文件夹.txt");
    //for (int i = 0; i < str.Length; i++)
    //{
    // Console.WriteLine(str[i]);
    //}
    //Console.ReadKey();
    #endregion


    #region ReadAllText
    //string str = File.ReadAllText(@"C:UsersdellDesktop新建文件夹.txt");
    //Console.WriteLine(str);
    //Console.ReadKey();
    #endregion


    //复制一个多媒体文件


    #region WriteAllBytes
    //先读取 再写入
    //byte[] buffer = File.ReadAllBytes(@"C:UsersdellDesktop好声音.wmv");

    //File.WriteAllBytes(@"C:UsersdellDesktop好声音复制.wmv", buffer);

    //Console.WriteLine("复制成功");
    //Console.ReadKey();
    #endregion


    #region WriteAllLines
    //File.WriteAllLines(@"C:UsersdellDesktop新的.txt", new string[] { "1", "2", "3", "4" });
    //Console.WriteLine("写入成功");
    //Console.ReadKey();
    #endregion


    #region WriteAllText
    //File.WriteAllText(@"C:UsersdellDesktop新的.txt", "看有没有覆盖掉");
    //Console.WriteLine("写入成功");
    //Console.ReadKey();
    #endregion


    File.AppendAllText(@"C:UsersdellDesktop新的123.txt","哈哈哈哈哈");
    Console.ReadKey();

    最强大的ReadAllBytes和WriteAllBytes 转换成2进制数据进行读写

    File只适用与小型的文件 代码简洁省时 大文件用FileStream

  • 相关阅读:
    CF1095F Make It Connected
    【ZJOI2016】旅行者
    [COCI 2009] OTOCI / 极地旅行社
    Sum in the tree
    MST Unification
    【集训队作业2018】Simple Tree
    [COCI2009]Dvapravca
    python 网络编程-02 多进程的交互案例
    python 日志模块logging
    python 网络编程-01基础
  • 原文地址:https://www.cnblogs.com/zhangliang1990/p/3405353.html
Copyright © 2011-2022 走看看