1. 程式人生 > >c#StreamReader讀取指定行的指定字串

c#StreamReader讀取指定行的指定字串

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 privatestaticint mintStart; 16 17 publicstaticvoid
Start() 18 { 19 mintStart = Environment.TickCount; 20 } 21 22 publicstaticlong Elapsed() 23 { 24 return Environment.TickCount - mintStart; 25 } 26 } 27 28 29 class MyFile 30 { 31 publicstaticstring 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 publicstaticstring 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 publicstaticstring 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(newchar[] { '/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 else114 { 115 return"Error! Can not find the line."; 116 } 117 streamReader.Close(); 118 fileStream.Close(); 119 return"Not Find!"; 120 } 121 } 122 staticvoid 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 constint 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