Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, June 12, 2013

File Pointer in StreamReader

If you are reading from file using StreamReader by using string fileContent = sr.ReadToEnd();
and now if you will try to read it again using while ((line = sr.ReadLine()) != null)  for the same object, then you will not get any single word as file pointer is on end of file due to sr.ReadToEnd();
StreamReader sr = new StreamReader(fullFilePath);

if (sr != null)
{
string fileContent = sr.ReadToEnd();

if (fileContent.Contains(",") || fileContent.Contains("-"))
{
     
StringBuilder sbZipRadius = new StringBuilder();
string line = string.Empty;

//Read file line by line
while ((line = sr.ReadLine()) != null)
{
//if ',' exists in the line,it will be replace by '-'
line = line.Replace(',', '-');
}
}

//Close the file
sr.Close();
sr.Dispose();

}

No comments:

Post a Comment