Many times, we always access text files in the same way as rows, and using a foreach statement can greatly simplify access logic: for example:
foreach (string line in New Linereader ("C:\abc.txt"))
Console.WriteLine (line);
The complete code is as follows:
Using System;
Using System.IO;
Using System.Text;
Using System.Collections;
Namespace Forks.Utils.IO
{
public struct linereader:idisposable
{
Public Linereader (string file, Encoding Encoding): This (file, Encoding, False)
{
}
Public Linereader (string file, Encoding Encoding, bool ignoreblanklines): This (new FileStream (file, FileMode.Open, Filea Ccess. Read, FileShare.Read), encoding, Ignoreblanklines)
{
}
Public Linereader (Stream stream, Encoding Encoding): This (stream, Encoding, false)
{
}
Public Linereader (Stream stream, Encoding Encoding, bool ignoreblanklines): This (new StreamReader (Stream, Encoding), IGN Oreblanklines)
{
}
Public Linereader (TextReader reader): This (reader, false)
{
}
TextReader Mreader;
BOOL Mignoreblanklines;
Public Linereader (TextReader reader, BOOL ignoreblanklines)
{
Mreader = reader;
Mignoreblanklines = Ignoreblanklines;
Mcurrent = null;
}
Public Linereader GetEnumerator ()
{
return this;
}
public void Reset ()
{
throw new NotSupportedException ("Linereaderö»äü¶áè¡ò»´î");
}
String mcurrent;
public string Current
{
Get
{
return mcurrent;
}
}
public bool MoveNext ()
{
Todo
{
Mcurrent = Mreader.readline ();
}while (mignoreblanklines && mcurrent!= null && mcurrent.length = 0);
return mcurrent!= null;
}
public void Dispose ()
{
Mreader.close ();
}
}
}
Test code:
Using System;
Using System.IO;
Using System.Text;
Using Nunit.framework;
Using Forks.test;
Namespace Forks.Utils.IO
{
[Testfixture]
public class Linereadertest
{
Const string testlines = @ "ABC asd EWR AFA E
Start with blanks
End With blanks
ººxöabc123!@#
End of text! ";
[Test]
public void Readfromreader ()
{
Dotest (New Linereader) (New StringReader (Testlines));
}
[Test]
public void ReadFromFile ()
{
string file = Path.gettempfilename ();
Try
{
Stringutil.savetofile (testlines, File, encoding.getencoding ("gb2312"));
Dotest (New Linereader (File, encoding.getencoding ("gb2312"));
}
Finally
{
Fileutil.safedelete (file);
}
}
[Test]
public void ReadFromStream ()
{
string file = Path.gettempfilename ();
Try
{
Stringutil.savetofile (testlines, File, encoding.getencoding ("gb2312"));
using (Stream stream = new FileStream (file, FileMode.Open))
Dotest (new Linereader (Stream, encoding.getencoding ("gb2312"));
}
Finally
{
Fileutil.safedelete (file);
}
}
void Dotest (Linereader reader)
{
StringBuilder sb = new StringBuilder ();
foreach (string line in reader)
Sb. Append (line + Environment.NewLine);
Assert.AreEqual (Testlines + Environment.NewLine, sb.) ToString ());
}
[Test]
public void Ignoreblankline ()
{
foreach (string line in New Linereader (new StringReader (Testlines), true)
Assert.istrue (line. Length!= 0);
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.