首页 > 代码库 > 读取TXT并筛选数据写入新建TXT

读取TXT并筛选数据写入新建TXT

  public void HandleTxt()
        {
            try
            {
                using (FileStream Du = new FileStream(TxtPath.Text, FileMode.Open))
                {
                    string P = TxtPath.Text; //源文件名*.log
                    string Name = P.Substring(P.Length - 14);   //原文件名
                    string Pa = P.Remove(P.Length - 14) + Name.Replace("log", "txt");   //新建同名txt
                    if (File.Exists(Pa))
                    {
                        MessageBox.Show("存在同名后缀为txt的文件,请删除" + Name.Replace("log", "txt"));
                        return;
                    }

                    FileStream Xie = new FileStream(Pa, FileMode.Create);   //新建
                    StreamReader sr = new StreamReader(Du, Encoding.Default); //读取
                    StreamWriter Wri = new StreamWriter(Xie, Encoding.Default);//写入

                    while (!sr.EndOfStream)
                    {
                        string sline = sr.ReadLine();
                        if (sline.IndexOf("合法刷卡") >= 0)
                        {
                            string[] u = sline.Split(();
                            Wri.WriteLine(u[2].Replace(")", ""));

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常!" + ex.Message);
            }

        }

 

读取TXT并筛选数据写入新建TXT