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

原文地址:https://www.cnblogs.com/gisdream/p/1962026.html