zoukankan      html  css  js  c++  java
  • IO操作

    1.写入文件

    using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
                        {
                            string name = "12345567778890";
                            byte[] bytes = Encoding.Default.GetBytes(name);
                            fileStream.Write(bytes, 0, bytes.Length);
                            fileStream.Flush();
                        }
    
    using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
                        {
                            StreamWriter sw = new StreamWriter(fileStream);
                            sw.WriteLine("1234567890");
                            sw.Flush();
                        }
    

      

    using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
                        {
                            string msg = "今天是Course6IOSerialize,今天上课的人有55个人";
                            sw.WriteLine(msg);
                            sw.Flush();
                        }
    

      

    using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
                        {
                            string name = "0987654321";
                            byte[] bytes = Encoding.Default.GetBytes(name);
                            sw.BaseStream.Write(bytes, 0, bytes.Length);
                            sw.Flush();
                        }
    

    2.文件读取

    foreach (string result in File.ReadAllLines(fileName))
                        {
                            Console.WriteLine(result);
                        }
                        string sResult = File.ReadAllText(fileName);
                        Byte[] byteContent = File.ReadAllBytes(fileName);
                        string sResultByte = System.Text.Encoding.UTF8.GetString(byteContent);
    

    3.大文件读取

    using (FileStream stream = File.OpenRead(fileName))//分批读取
                        {
                            int length = 5;
                            int result = 0;
    
                            do
                            {
                                byte[] bytes = new byte[length];
                                result = stream.Read(bytes, 0, 5);
                                for (int i = 0; i < result; i++)
                                {
                                    Console.WriteLine(bytes[i].ToString());
                                }
                            }
                            while (length == result);
                        }
    

      

      

  • 相关阅读:
    北京集训:20180325
    北京集训:20180323
    北京集训:20180321
    北京集训:20180320
    Bzoj5209[Tjoi2012]防御:姿势题
    北京集训:20180318
    北京集训:20180316
    北京集训:20180314
    北京集训:20180313
    Bzoj4818:生成函数 快速幂
  • 原文地址:https://www.cnblogs.com/ITanyx/p/9064631.html
Copyright © 2011-2022 走看看