Simplify the access of the text file using Foreach.

xiaoxiao2021-03-06  93

Many times, we always access text files in a row, using the Foreach statement to greatly simplify access logic: for example:

Console.Writeline (LINE);

The full 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 IgnorebLines): This (New Filestream (File, FileMode.open, FileAccess.Read, Fileshare.Read), Encoding, IgnorebLines

{

}

Public LineReader (stream stream, eNCoding): this (stream, encoding, false)

{

}

Public LineReader (Stream Stream, Encoding Encoding, Bool IgnoreBLines): This (New Stream, Encoding), IgnoreBlanklines

{

}

Public LineReader: this (Reader, False)

{

}

TextReader MReader;

Bool mignoreblankline;

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, PUBLIC STRING CURRENT

{

get

{

Return McURRENT;

}

}

Public bool movenext ()

{

DO

{

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

O × öabc123! @ #

END OF TEXT! "

[TEST]

Public void readfromreader ()

{

New LineReader (New StringReader (Testlines));

}

[TEST]

Public void readfromfile ()

{

String file = path.getTempFileName ();

Try

{

Stringutil.savetofile (Testline, File, Encoding.Getencoding ("GB2312"));

Dotest (New LineReader (File, Encoding.Getencoding);

}

Finally

{

Fileutil.safedeete (file);

}

}

[TEST]

Public void readfromstream ()

{

String file = path.getTempFileName ();

Try

{

Stringutil.savetofile (Testline, File, Encoding.Getencoding ("GB2312"));

USING (Stream Stream = New FileStream (File, FileMode.Open))

Dotest (New LineReader (stream, eNCoding.getencoding)))

}

Finally

{

Fileutil.safedeete (file);

}

}

Void Dotest (LineReader Reader)

{

Stringbuilder SB = New StringBuilder ();

FOREACH (String Line in Reader)

sb.append (line environment.newline);

AskERT.Arequal (Testlines Environment.newline, sb.tostring ());

}

[TEST]

Public void ignoreblankline ()

{

FOREACH (String Line in New LineRead (New StringReader (Testlines), TRUE)

AskERT.ISTRUE (Line.Length! = 0);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-123361.html

New Post(0)