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

    /// <summary>
           
    /// 将存贮在文本文件中的数据绑定到DropDownList中  test,test
           
    /// </summary>
           
    /// <param name="drdlst">下拉菜单</param>
           
    /// <param name="filePath">虚拟路径</param>
            public void Drdlst_BindDataByTxt1(DropDownList drdlst, string filePath)
            {
               
    string fileName = HttpContext.Current.Server.MapPath(filePath);
               
    string content = "";
                StreamReader sr
    = null;
               
    try
                {
                   
    //打开文件并显示其内容
                    sr = new StreamReader(fileName, System.Text.Encoding.Default);
                   
    for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())
                    {
                        content
    += line.ToString();
                    }
                   
    string[] arr = content.Split(',');
                   
    for (int i = 0; i < arr.Length; i++)
                    {
                        ListItem li
    = new ListItem();
                        li.Text
    = arr[i].ToString();
                        li.Value
    = arr[i].ToString();
                        drdlst.Items.Add(li);
                    }
                }
               
    catch (IOException ee)
                {
                    HttpContext.Current.Response.Write(
    "<script>alert(" + ee.Message + ")</script>");
                }
               
    finally
                {
                   
    if (sr != null)
                    {
                        sr.Close();
                    }
                }
            }

           
    /// <summary>
           
    /// 获取txt中信息
           
    /// </summary>
           
    /// <param name="filePath">虚拟路径</param>
           
    /// <returns></returns>
            public string GetTxtInfo(string filePath)
            {
               
    string fileName = HttpContext.Current.Server.MapPath(filePath);
               
    string str = "";
                StreamReader sr
    = null;
               
    try
                {
                    sr
    = new StreamReader(fileName, System.Text.Encoding.Default);
                   
    for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())
                    {
                        str
    += line.ToString();
                    }
                }
               
    catch (IOException ee)
                {
                    HttpContext.Current.Response.Write(
    "<script>alert(" + ee.Message + ")</script>");
                }
               
    finally
                {
                   
    if (sr != null)
                    {
                        sr.Close();
                    }
                }

               
    return str;
            }

           
    /// <summary>
           
    /// 保存数据到txt中
           
    /// </summary>
           
    /// <param name="str">要保存的数据</param>
           
    /// <param name="filePath">虚拟路径</param>
            public void SaveTxtInfo(string str, string filePath)
            {
               
    string fileName = HttpContext.Current.Server.MapPath(filePath);
                StreamWriter sw
    = null;
                FileStream oFileStream
    = null;
               
    try
                {
                   
    if (!System.IO.File.Exists(fileName))
                    {
                        oFileStream
    = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                    }
                   
    else
                    {
                        oFileStream
    = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                    }

                    sw
    = new StreamWriter(oFileStream, Encoding.Default);
                    sw.Write(str);
                    sw.WriteLine();
                }
               
    catch (IOException ee)
                {
                    HttpContext.Current.Response.Write(
    "<script>alert(" + ee.Message + ")</script>");
                }
               
    finally
                {
                   
    if (sw != null)
                    {
                        sw.Close();
                        oFileStream.Close();
                    }
                }
            }

  • 相关阅读:
    【Nginx】ngx_event_core_module模块
    ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
    nyist oj 214 单调递增子序列(二) (动态规划经典)
    java 入门书籍(java7)
    ARCGIS将WGS84坐标投影到高斯平面
    【linux】linux下对java程序生成dump文件,并使用IBM Heap Analyzer进行分析,查找定位内存泄漏的问题代码
    【springboot】【socket】spring boot整合socket,实现服务器端两种消息推送
    【linux】linux修改open file 大小
    【docker】docker限制日志文件大小的方法+查看日志文件的方法
    【docker】docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1762860.html
Copyright © 2011-2022 走看看