开发者

Using streamreader to read line containing this "//"?

Read a Text file having any line starts from 开发者_开发技巧"//" omit this line and moved to next line. The Input text file having some seprate partitions. Find line by line process and this mark.


If you are using .Net 3.5 you can use LINQ with a IEnumerable wrapped around a Stream Reader. This cool part if then you can just use a where statement to file statmens or better yet use a select with a regular expression to just trim the comment and leave data on the same line.

//.Net 3.5
static class Program
{
    static void Main(string[] args)
    {
        var clean = from line in args[0].ReadAsLines()
                    let trimmed = line.Trim()
                    where !trimmed.StartsWith("//")
                    select line;
    }
    static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}

...

//.Net 2.0
static class Program
{
    static void Main(string[] args)
    {
        var clean = FilteredLines(args[0]);
    }
    static IEnumerable<string> FilteredLines(string filename)
    {
        foreach (var line in ReadAsLines(filename))
            if (line.TrimStart().StartsWith("//"))
                yield return line;
    }
    static IEnumerable<string> ReadAsLines(string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}


I'm not sure what you exactly need but, if you just want to filter out // lines from some text in a stream... just remember to close the stream after using it.

public string FilterComments(System.IO.Stream stream)
        {
            var data = new System.Text.StringBuilder();
            using (var reader = new System.IO.StreamReader(stream))
            {
                var line = string.Empty;
                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    if (!line.TrimStart(' ').StartsWith("//"))
                    {
                        data.Append(line);
                    }
                }
            }

            return data.ToString();
        }


Class SplLineIgnorStrmReader:StreamReader  // derived class from StreamReader

SplLineIgnorStrmReader ConverterDefFileReadStream = null;
{
//created the Obj for this Class.
Obj = new SplLineIgnorStrmReader(strFile, Encoding.default);
}

public override string ReadLine()
        {
            string strLineText = "", strTemp;
            while (!EndOfStream)
            {
                strLineText = base.ReadLine();
                strLineText = strLineText.TrimStart(' ');
                strLineText = strLineText.TrimEnd(' ');
                strTemp = strLineText.Substring(0, 2);
                if (strTemp == "//")
                    continue;
                break;

            }
            return strLineText;

This is if u want to read the Text file and omit any comments from that file(here exclude "//" comment).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜