INDEX

C# - text file read and search

Find the line does not need search to the contents of a text file.

using System;
using System.IO;
using System.Text;
using System.Collections;

class study {
  static void Main() {

    int i;
    bool bSrh;
    string line;

    string[] strAry = new string[]
    {
      "apple","cherry","banana","grape"
    };
    
    System.IO.StreamWriter sw = new System.IO.StreamWriter(
        @"result.txt", false, System.Text.Encoding.GetEncoding("shift_jis"));

    using (StreamReader sr = new StreamReader(
        "access.log", Encoding.GetEncoding("Shift_JIS")))
    {
      while ((line = sr.ReadLine()) != null)
      {
        bSrh = false;
        for (i = 0; i < strAry.Length; i++)
        {
          if (line.IndexOf(strAry[i]) >= 0) 
          {
            bSrh = true;
            break;
          }
        }
        if (bSrh == false)
        {
          sw.WriteLine(line);
        }
      }
    }
    
    sw.Close();
    
  }
}



Copyright(c) 2007-2023 coding.dojeun.com All Rights Reserved.