How to read StreamReader text line by line - c#

I have a text file hosted and this file have a organized string like this:
First line
Second line
Third line
Fourth line
Sixth line
Seventh line
....................
I'm getting all content from this file with following function:
private static List<string> lines;
private static string DownloadLines(string hostUrl)
{
var strContent = "";
var webRequest = WebRequest.Create(hostUrl);
using (var response = webRequest.GetResponse())
using (var content = response.GetResponseStream())
using (var reader = new StreamReader(content))
{
strContent = reader.ReadToEnd();
}
lines = new List<string>();
lines.Add(strContent);
return strContent;
}
// Button_click
DownloadLines("http://address.com/folder/file.txt");
for (int i = 0; i < lines.Count; i++)
{
lineAux = lines[0].ToString();
break;
}
Console.WriteLine(lineAux);
Then, how I can access for example the first index like text inside this large organized string that is returned by this method?

You can read text file line by line this way
private static List<string> DownloadLines(string hostUrl)
{
List<string> strContent = new List<string>();
var webRequest = WebRequest.Create(hostUrl);
using (var response = webRequest.GetResponse())
using (var content = response.GetResponseStream())
using (var reader = new StreamReader(content))
{
while (!reader.EndOfStream)
{
strContent.Add(reader.ReadLine());
}
}
return strContent;
}
after returning this list from the method you can access the text line using list index

Related

Read an Excel File from an Amazon S3 Bucket using c#

I'm trying to read excel file from my S3 bucket. In the Response Stream, I am getting values like "PK\u0003\u0004\n\0\0\0\0\0�N0\0\0\0\0\t\0\0\0docProps". Could anyone help to map the stream to a data table or convert to string. And also when I see quick watch, the Read and Write Timeout has thrown some errors.
using (var _client = new AmazonS3Client(accKey, secKey, Amazon.RegionEndpoint.USEast1))
using (var response1 = await _client.GetObjectAsync("rrrrr","mmm.xls"))
using (var responseStream = response1.ResponseStream)
using (var reader = new StreamReader(responseStream))
{
var title = response1.Metadata["x-amz-meta-title"];
var contentType = response1.Headers["Content-Type"];
responseBody = reader.ReadToEnd();
string line;
string[] columns = null;
// Here the reader.ReadLine receiving only null values
while ((line = reader.ReadLine()) != null)
{
columns = line.Split(',');
string col1 = columns[0]; }
}

C# exception and wrong formatting in Console output after changing Streamread path to resource file

First im sory for the bad english and this is my first question here. pls excuse if im doing something wrong.
Im reading data from a textfile using the streamreader. now I made the file a resource and the output formatting has changed.
Reading the textfile before:
var assembly = Assembly.Load("WordFinder.Data");
var resourceName = "WordFinder.Data.Data.WordsDictionaryFinalUppercase-de-DE.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
for (string currentWord = reader.ReadLine(); currentWord != null; currentWord = reader.ReadLine())
{...
output method:
internal static void PrintWordList(IEnumerable<string> wordList, out int printedWordsCount)
{
printedWordsCount = 0;
foreach (var word in wordList)
{
printedWordsCount++;
Console.Write("{0,-40}", word);
}
Console.WriteLine();
}
Output is as expected:
Word1 Word2 Word3
now I change the path for the streamreader to resource:
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(Data.Properties.Resources.Dict_de))
{
for (string currentWord = reader.ReadLine(); currentWord != null; currentWord = reader.ReadLine()
{....
output:
Word1
Word2
Word3
an a exception is thrown for ashort moment but the app continues running.
Any ideas whats going on?

Read last line from website without saving file on disk

I have a website with many large CSV files (up to 100,000 lines each). From each CSV file, I need to read the last line in the file. I know how to solve the problem when I save the file on disk before reading its content:
var url = "http://data.cocorahs.org/cocorahs/export/exportreports.aspx?ReportType=Daily&Format=csv&Date=1/1/2000&Station=UT-UT-24"
var client = new System.Net.WebClient();
var tempFile = System.IO.Path.GetTempFileName();
client.DownloadFile(url, tempFile);
var lastLine = System.IO.File.ReadLines(tempFile).Last();
Is there any way to get the last line without saving a temporary file on disk?
I tried:
using (var stream = client.OpenRead(seriesUrl))
{
using (var reader = new StreamReader(stream))
{
var lastLine = reader.ReadLines("file.txt").Last();
}
}
but the StreamReader class does not have a ReadLines method ...
StreamReader does not have a ReadLines method, but it does have a ReadLine method to read the next line from the stream. You can use it to read the last line from the remote resource like this:
using (var stream = client.OpenRead(seriesUrl))
{
using (var reader = new StreamReader(stream))
{
string lastLine;
while ((lastLine = reader.ReadLine()) != null)
{
// Do nothing...
}
// lastLine now contains the very last line from reader
}
}
Reading one line at a time with ReadLine will use less memory compared to StreamReader.ReadToEnd, which will read the entire stream into memory as a string. For CSV files with 100,000 lines this could be a significant amount of memory.
This worked for me, though the service did not return data (Headers of CSV only):
public void TestMethod1()
{
var url = "http://data.cocorahs.org/cocorahs/export/exportreports.aspx?ReportType=Daily&Format=csv&Date=1/1/2000&Station=UT-UT-24";
var client = new System.Net.WebClient();
using (var stream = client.OpenRead(url))
{
using (var reader = new StreamReader(stream))
{
var str = reader.ReadToEnd().Split('\n').Where(x => !string.IsNullOrEmpty(x)).LastOrDefault();
Debug.WriteLine(str);
Assert.IsNotEmpty(str);
}
}
}

Get substring from MemoryStream without converting entire stream to string

I would like to be able to efficiently get a substring from a MemoryStream (that originally comes from a xml file in a zip). Currently, I read the entire MemoryStream to a string and then search for the start and end tags of the xml node I desire. This works fine but the text file may be very large so I would like to avoid converting the entire MemoryStream into a string and instead just extract the desired section of xml text directly from the stream.
What is the best way to go about this?
string xmlText;
using (var zip = ZipFile.Read(zipFileName))
{
var ze = zip[zipPath];
using (var ms = new MemoryStream())
{
ze.Extract(ms);
ms.Position = 0;
using(var sr = new StreamReader(ms))
{
xmlText = sr.ReadToEnd();
}
}
}
string startTag = "<someTag>";
string endTag = "</someTag>";
int startIndex = xmlText.IndexOf(startTag, StringComparison.Ordinal);
int endIndex = xmlText.IndexOf(endTag, startIndex, StringComparison.Ordinal) + endTag.Length - 1;
xmlText = xmlText.Substring(startIndex, endIndex - startIndex + 1);
If your file is a valid xml file then you should be able to use a XmlReader to avoid loading the entire file into memory
string xmlText;
using (var zip = ZipFile.Read(zipFileName))
{
var ze = zip[zipPath];
using (var ms = new MemoryStream())
{
ze.Extract(ms);
ms.Position = 0;
using (var xml = XmlReader.Create(ms))
{
if(xml.ReadToFollowing("someTag"))
{
xmlText = xml.ReadInnerXml();
}
else
{
// <someTag> not found
}
}
}
}
You'll likely want to catch potential exceptions if the file is not valid xml.
Assuming that since it is xml it will have line breaks, it would probably be best to use StreamReader ReadLine and search for your tags in each line. (Also note put your StreamReader in a using as well.)
Something like
using (var ms = new MemoryStream())
{
ze.Extract(ms);
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
bool adding = false;
string startTag = "<someTag>";
string endTag = "</someTag>";
StringBuilder text = new StringBuilder();
while (sr.Peek() >= 0)
{
string tmp = sr.ReadLine();
if (!adding && tmp.Contains(startTag))
{
adding = true;
}
if (adding)
{
text.Append(tmp);
}
if (tmp.Contains(endTag))
break;
}
xmlText = text.ToString();
}
}
This assumes that the start and end tags are on a line by themselves. If not, you could clean up the resulting text string by getting the index of start and end again like you originally did.

Change a line in a file containing a specific String in C#

ive got a problem: i want to find a line containing a certain string, but i only know how to replace the string in the file or all the lines, i know the command "string.Contains", but it doesnt seem to work properly as i use it: i tried to use "if (data.contains(string))", but then it still changes all the lines to that string. heres my code:
private void button1_Click(object sender, EventArgs e)
{
string replaceText = "peter";
string withText = "Gilbert";
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(#"C:\Users\G\Documents\test.txt"))
{
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(#"C:\Users\G\Documents\test.txt" + ".tmp"))
{
while (!streamReader.EndOfStream)
{
string data = streamReader.ReadLine();
data = data.Replace(replaceText, withText);
streamWriter.WriteLine(data);
}
}
}
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(#"C:\Users\G\Documents\test.txt" + ".tmp"))
{
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(#"C:\Users\G\Documents\test.txt"))
{
while (!streamReader.EndOfStream)
{
string data = streamReader.ReadLine();
data = data.Replace(replaceText, withText);
streamWriter.WriteLine(data);
}
}
}
}
}
Try this:
FileStream stream = File.Open("file", FileMode.Open);
StreamReader rdr = new StreamReader(rdr);
String[] flines = rdr.ReadToEnd().Split(new String[]{"\r\n"}, StringSplitOptions.None);
rdr.Close();
stream = File.Create("file");
StreamWriter wtr = new StreamWriter(stream);
foreach(String str in flines)
{
wtr.WriteLine(str.Replace(replaceTxt, newText));
}
wtr.Close();
Of course you could put logic in the loop the either change or not change the string written based on whatever criterion you like.

Categories