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();
                    }
                }
            }

  • 相关阅读:
    截屏 多难未遂
    捕捉异常
    Android中缓存记忆
    Android中的线程池
    悄悄为Android中解决部分适配问题哦!
    java中的服务端跳转与客户端跳转区别与联系
    doget(),doput()方法的使用
    基本概念--同步,异步
    java--SimpleDataFormat类
    java--9
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1762860.html
Copyright © 2011-2022 走看看