c#StreamReader读取指定行的指定字符串

http://blog.csdn.net/Reyes11235813/article/details/5493331

问题:

在一个文件的自定行(LineNum)之后查找一个指定的字符串(queryString).

一下是我想到的两种办法和朋友的一种办法,希望看到这篇文章的大侠们也能提提建议。

现在的三中办法中算是第二种方法跑起来最高效了。一下是他们分别循环10,100,1000次的平均运行时间。

Processing 100%
counts: 10
avgCostTime1: 51ms
avgCostTime2: 25ms
avgCostTime3: 37ms
-------------------------
Processing 100%
counts: 100
avgCostTime1: 33ms
avgCostTime2: 17ms
avgCostTime3: 31ms
-------------------------
Processing 100%
counts: 1000
avgCostTime1: 39ms
avgCostTime2: 21ms
avgCostTime3: 37ms
-------------------------

 

Code:
   
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Reflection; 6 using System.IO; 7 8 9 namespace test 10 { 11 class Program 12 { 13 class StopWatch 14 { 15 private static int mintStart; 16 17 public static void Start() 18 { 19 mintStart = Environment.TickCount; 20 } 21 22 public static long Elapsed() 23 { 24 return Environment.TickCount - mintStart; 25 } 26 } 27 28 29 class MyFile 30 { 31 public static string search1( int lineNum, string queryString, string filePath) 32 { 33 StringBuilder stringBillder = new StringBuilder(); 34 FileStream fileStream = new FileStream(filePath, FileMode.Open); 35 StreamReader streamReader = new StreamReader(fileStream); 36 int currentLine = 1 ; 37 while ( - 1 != (streamReader.Peek())) 38 { 39 if (currentLine < lineNum) 40 { 41 currentLine ++ ; 42 continue ; 43 } 44 else 45 { 46 string tempLine = streamReader.ReadLine(); 47 stringBillder.Append(tempLine); 48 currentLine ++ ; 49 } 50 } 51 streamReader.Close(); 52 fileStream.Close(); 53 int tempIndex = stringBillder.ToString().IndexOf(queryString); 54 if (tempIndex != - 1 ) 55 { 56 return " Find! " + tempIndex; 57 } 58 else 59 { 60 return " Not Find! " ; 61 } 62 } 63 64 public static string search2( int lineNum, string queryString, string filePath) 65 { 66 FileStream fileStream = new FileStream(filePath, FileMode.Open); 67 StreamReader streamReader = new StreamReader(fileStream); 68 int currentLine = 1 ; 69 while ( - 1 != (streamReader.Peek())) 70 { 71 if (currentLine < lineNum) 72 { 73 currentLine ++ ; 74 continue ; 75 } 76 else 77 { 78 int tempIndex = streamReader.ReadLine().IndexOf(queryString); 79 if (tempIndex != - 1 ) 80 { 81 streamReader.Close(); 82 fileStream.Close(); 83 return " Find! Line " + currentLine + " cols " + tempIndex; 84 } 85 currentLine ++ ; 86 } 87 } 88 streamReader.Close(); 89 fileStream.Close(); 90 return " Not Find! " ; 91 } 92 93 public static string search3( int lineNum, string queryString, string filePath) 94 { 95 FileStream fileStream = File.Open(filePath, FileMode.Open); 96 StreamReader streamReader = new StreamReader(fileStream); 97 string log = streamReader.ReadToEnd(); 98 string [] arr = log.Split( new char [] { ' /r ' , ' /n ' }); 99 100 if (arr != null && arr.Length >= lineNum) 101 { 102 for ( int i = lineNum; i <= arr.Length; i ++ ) 103 { 104 int tempIndex = arr[i].IndexOf(queryString); 105 if (tempIndex != - 1 ) 106 { 107 streamReader.Close(); 108 fileStream.Close(); 109 return " Found! Line: " + i + " Cols: " + tempIndex; 110 } 111 } 112 } 113 else 114 { 115 return " Error! Can not find the line. " ; 116 } 117 streamReader.Close(); 118 fileStream.Close(); 119 return " Not Find! " ; 120 } 121 } 122 static void Main( string [] args) 123 { 124 string filePath = @" d:/WindowsUpdate.log " ; 125 long costTime1 = 0L ; 126 long costTime2 = 0L ; 127 long costTime3 = 0L ; 128 string result = null ; 129 const int counts1 = 10 ; 130 131 132 for ( int i = 0 ; i < 3 ; i ++ ) 133 { 134 int tempCount = ( int )Math.Pow(counts1, i + 1 ); 135 for ( int j = 0 ; j < tempCount; j ++ ) 136 { 137 StopWatch.Start(); 138 result = MyFile.search1( 800 , @" Process: C:/Windows/system32/svchost.exe " , filePath); 139 costTime1 += StopWatch.Elapsed(); 140 // Console.Out.WriteLine(result); 141 // Console.Out.WriteLine(costTime + "ms"); 142 143 StopWatch.Start(); 144 result = MyFile.search2( 800 , @" Process: C:/Windows/system32/svchost.exe " , filePath); 145 costTime2 += StopWatch.Elapsed(); 146 // Console.Out.WriteLine(result); 147 // Console.Out.WriteLine(costTime + "ms"); 148 149 StopWatch.Start(); 150 result = MyFile.search3( 800 , @" Process: C:/Windows/system32/svchost.exe " , filePath); 151 costTime3 += StopWatch.Elapsed(); 152 // Console.Out.WriteLine(result); 153 // Console.Out.WriteLine(costTime + "ms"); 154 Console.Write( " Processing {0}%/r " , (j + 1 ) * 100 / tempCount); 155 } 156 Console.Out.WriteLine( " /ncounts: " + tempCount); 157 Console.Out.WriteLine( " avgCostTime1: " + costTime1 / tempCount + " ms " ); 158 Console.Out.WriteLine( " avgCostTime2: " + costTime2 / tempCount + " ms " ); 159 Console.Out.WriteLine( " avgCostTime3: " + costTime3 / tempCount + " ms " ); 160 Console.Out.WriteLine( " ------------------------- " ); 161 } 162 163 164 165 Console.ReadLine(); 166 } 167 } 168 } 169
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页