zoukankan      html  css  js  c++  java
  • mvc 读写txt文档

    -----------------写入内容----------------

    string userfile = "UserData.txt";
    StreamWriter sw = null;

    //判断是否存在
    if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(@"../" + userfile)))
    {

      //不存在,新建
      System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath(@"../" + userfile)).Close();
      sw = new StreamWriter(Server.MapPath(@"../" + userfile), true, System.Text.Encoding.Default);

      sw.Write("用户名");
      sw.Write("," + "手机号");
      sw.Write("," + "地址");
      sw.WriteLine();
    }
    else
    {
      sw = new StreamWriter(Server.MapPath(@"../" + userfile), true, System.Text.Encoding.Default);
    }

      sw.Write(“想要写入的内容”);
      sw.WriteLine();
      sw.Close(); //关闭流

    -----------------读取内容----------------

    string uListPath = @"../" + "UserData.txt";

    string content = string.Empty;
    if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(uListPath)))
    {
      //文件不存在
    }
    else
    {
      using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(uListPath),   System.Text.Encoding.Default))
      {
        content = sr.ReadToEnd(); // 读取txt文件内容
      }
    }

  • 相关阅读:
    用linux搭建ranzhi环境
    软件测试知识点总结
    python函数
    python基础
    数据库学习之四
    数据库学习之三
    数据库学习之二
    git常用命令
    JS方法总结
    原生javascript请求服务器详解
  • 原文地址:https://www.cnblogs.com/yao3364/p/8708550.html
Copyright © 2011-2022 走看看