zoukankan      html  css  js  c++  java
  • C#文件操作

    try
    {
    string directory = System.AppDomain.CurrentDomain.BaseDirectory + "logs";
    string fileName = System.AppDomain.CurrentDomain.BaseDirectory + "logs\amount.txt";

    if (File.Exists(fileName))
    {
    //读取
    StreamReader m_streamReader = new StreamReader(fileName, Encoding.GetEncoding("GB2312"), true);
    m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
    int[] amount = new int[2];
    string strLine;
    for (int i = 0; i < 2; i++)
    {
    strLine = m_streamReader.ReadLine();
    if (strLine != null)
    {
    amount[i] = Int32.Parse(strLine.Split(':')[1]);
    Console.WriteLine(amount[i]);
    }
    }
    m_streamReader.Close();
    m_streamReader.Dispose();

    amount[rightOrWrong]++;

    //写入
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);
    //开始写入
    sw.WriteLine("right:" + amount[0]);
    sw.WriteLine("wrong:" + amount[1]);
    //清空缓冲区
    sw.Flush();
    //关闭流
    sw.Close();
    fs.Close();
    }
    else
    {
    if (!Directory.Exists(directory))
    {
    Directory.CreateDirectory(directory);
    }
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);
    //开始写入
    sw.WriteLine("right:1");
    sw.WriteLine("wrong:1");
    //清空缓冲区
    sw.Flush();
    //关闭流
    sw.Close();
    fs.Close();
    }

    //Console.ReadKey();

    //fs.Close();
    //fs.Dispose();
    }
    catch (Exception ex)
    {
    LogHelper2.Exception(ex.Message);
    throw;
    }

  • 相关阅读:
    Oracle数据库管理
    Oracle——范式
    GUID
    java课上知识点整理—语句
    java课上知识点整理—java代码结构、标识符、数据类型、运算符
    使用css实现时间轴
    超简单的轮播实现
    第一个vue示例-高仿微信
    12. thymeleaf中资源相对路径的解决
    11. 将博客部署到tomcat上
  • 原文地址:https://www.cnblogs.com/lcyuhe/p/4878378.html
Copyright © 2011-2022 走看看