zoukankan      html  css  js  c++  java
  • 文件流读写

    string FolderPath = "d:" + "//Logs";
    //判断文件是否存在,否则创建;
    if (Directory.Exists(FolderPath) == false)
    {
    Directory.CreateDirectory(FolderPath);
    }

    string year = DateTime.Now.Year.ToString().Trim();
    string month = DateTime.Now.Month.ToString().Trim();
    string day = DateTime.Now.Day.ToString().Trim();

    string FilePath= FolderPath+"//log" +year + "-" + month + "-" + day + ".txt";
    if (File.Exists(FilePath) == false)
    {
    //创建文件
    FileStream fs123 = File.Create(FilePath);
    fs123.Close();

    }

    //if (File.Exists(FilePath) == true)
    //{
    // //清除TXT文本的内容
    // FileStream stream2 = File.Open(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
    // stream2.Seek(0, SeekOrigin.Begin);
    // stream2.SetLength(0); //清空txt文件
    // stream2.Close();
    //}

    MSG = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " : " + MSG + " ";

    //重新记录txt内容
    FileStream fs = new FileStream(FilePath, FileMode.Append);
    //获取字节数组
    byte[] data = System.Text.Encoding.Default.GetBytes(MSG);
    //开始写入
    fs.Write(data, 0, data.Length);
    //清空缓冲区,关闭流
    fs.Flush();
    fs.Close();

  • 相关阅读:
    内部类的作用
    zookeeper(1)-概述
    @RequestBody、@ResponseBody注解是如何将输入输出转换成json的
    HashMap之红黑树
    HashMap深入理解
    SpringBoot的四种定时任务
    Redis基础
    Redis内存回收机制
    高频面试题
    36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9746344.html
Copyright © 2011-2022 走看看