• c#读写txt文本


    (1)将污染源对象以一定的格式保存到txt文本中

      public bool SaveToTxtFile(List<pollutionSource> psList)
            {
                bool bResult = false;
                System.IO.FileStream FS = null;
                System.IO.StreamWriter SW = null;
                try
                {
                    //新建文件流
                    FS = new System.IO.FileStream(Application.StartupPath + "/Data/info1.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    //建立文件对应的输入流
                    SW = new System.IO.StreamWriter(FS);
                    //向输入流中写入信息
                    SW.Write(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\r\n",
        "Name", "Type", "Place", "Date"));
                    foreach (pollutionSource ps in psList)
                    {
                        SW.Write(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\r\n",
                            ps.Name, ps.Type, ps.Place, ps.Date));
                    }
                    bResult = true;
                }
                catch
                {
                    bResult = false;
                }
                finally
                {
                    if (SW != null)
                    {
                        SW.Close();
                    }
                    if (FS != null)
                    {
                        FS.Close();
                    }
                }
                return bResult;
            }

    (2)读取txt文本中的信息

     public List<TULI> ReadTxtFile(string colorFileName,string gradeFileName)
            {
                List<TULI> tulis = new List<TULI>();

                System.IO.FileStream FS = null;
                System.IO.StreamReader SR = null;
                if (System.IO.File.Exists(colorFileName) & System.IO.File.Exists(gradeFileName))
                {
                    try
                    {
                        if (tulis == null)
                            tulis = new List<TULI>();
                        //读入颜色信息
                        //using自动释放资源
                        using (FS = new System.IO.FileStream(colorFileName, System.IO.FileMode.Open))
                        {
                            SR = new System.IO.StreamReader(FS, System.Text.Encoding.Default);
                            string Line = null;
                            System.Drawing.Color color = new System.Drawing.Color();
                            string[] rgb = new string[4];

                            for (Line = SR.ReadLine(); Line != null; Line = SR.ReadLine())
                            {
                                if (!(Line.Trim() == ""))
                                {
                                    TULI tuli = new TULI();
                                    rgb = Line.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries);
                                    tuli.color = System.Drawing.Color.FromArgb(Convert.ToInt32(rgb[0]), Convert.ToInt32(rgb[1]), Convert.ToInt32(rgb[2]));
                                    //tuli.value = rgb[3];
                                    tulis.Add(tuli);
                                }
                            }
                            //读入分级信息
                            FS = new System.IO.FileStream(gradeFileName, System.IO.FileMode.Open);
                            SR = new System.IO.StreamReader(FS, System.Text.Encoding.Default);

                            int i = 0;
                            string Line1 = null;
                            for (Line = SR.ReadLine(); Line != null; )
                            {
                                Line1 = SR.ReadLine();
                                if (Line1 != null)
                                {
                                    tulis[i].value = Line + "-" + Line1;
                                }
                                i++;
                                Line = Line1;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (SR != null)
                        {
                            SR.Dispose();
                            SR.Close();
                        }
                        if (FS != null)
                        {
                            FS.Dispose();
                            FS.Close();
                        }
                    }
                }
                else
                {
                    tulis = null;
                }
                return tulis;
            }

  • 相关阅读:
    mysql的CURRENT_TIMESTAMP【转】
    php开发中emoji表情的问题3种方法轻松处理【转】
    JavaScript 正则表达式【转】
    使用 内置函数strtok()函数实现 loadrunner 字符串替换
    python打开文件失败,报错'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
    txt文本程序 打开python文件 另存为原来的文件名,不能覆盖原来的文件解决
    linux 文件解压
    tar.xz 解压
    设置xampp开机自动启动
    Can’t connect to local MySQL server through socket的解决方法
  • 原文地址:https://www.cnblogs.com/gisdream/p/1962026.html
Copyright © 2020-2023  润新知