C#跳过文本文件的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2134700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:47:40  来源:igfitidea点击:

C# skipping first line of a text file

c#

提问by

here is the code i'm using

这是我正在使用的代码

        using (FileStream fs = new FileStream(filename, FileMode.Open))
        using (StreamReader rdr = new StreamReader(fs))
        {
            while (!rdr.EndOfStream)
            {
                for (int z = 0; z < 2; z++)
                {

                    string[] lines = rdr.ReadLine().Split('|');
                    {
                        sb.AppendLine(";Re");
                        sb.AppendLine("@C PAMT " + lines[3]);
                        sb.AppendLine("@T " + lines[0]);
                        sb.AppendLine("@D @I\" + lines[1]).Replace("I:\", "");
                        sb.AppendLine(lines[2].Replace(";", "\r\n");
                    }
                }
            }
        }

        using (FileStream fs = new FileStream(outputfilename, FileMode.Create))
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(sb.ToString());
        }

All i want is to either skip the first line of the StreamReader or skip the entire first stringbuilder. I thought the for would do it but it doesnt.

我想要的只是跳过 StreamReader 的第一行或跳过整个第一个 stringbuilder。我以为 for 会这样做,但它没有。

Note: i'd like to advoid a foreach.

注意:我想避免使用 foreach。

采纳答案by rball

Why not just read the first line and do nothing with it?

为什么不只阅读第一行而不做任何处理呢?

using (StreamReader rdr = new StreamReader(fs))
{
  rdr.ReadLine();
  ...

or if you don't want to do that, have an outside variable i and test for i!=0

或者,如果您不想这样做,请使用外部变量 i 并测试 i!=0

回答by JaredPar

Why not justn add the following line immediately after creating the rdrvariable?

为什么不在创建rdr变量后立即添加以下行?

if ( !rdr.EndOfStream ) {rdr.ReadLine();}

回答by Joel Etherton

How about

怎么样

if(z > 0)
{
  // All of your sb.Append stuff
}

EDIT:

编辑:

using (FileStream fs = new FileStream(filename, FileMode.Open)) 
using (StreamReader rdr = new StreamReader(fs)) 
{  
    bool passedFirstline = false;
    while (!rdr.EndOfStream) 
    { 
        string[] lines = rdr.ReadLine().Split('|');
        if(passedFirstLine)                     
        { 
            sb.AppendLine(";Re"); 
            sb.AppendLine("@C PAMT " + lines[3]); 
            sb.AppendLine("@T " + lines[0]); 
            sb.AppendLine("@D @I\" + lines[1]).Replace("I:\", ""); 
            sb.AppendLine(lines[2].Replace(";", "\r\n"); 
        } 
        else
        {
            passedFirstLine = true;
        }
    } 
} 

using (FileStream fs = new FileStream(outputfilename, FileMode.Create)) 
using (StreamWriter writer = new StreamWriter(fs)) 
{ 
   writer.Write(sb.ToString()); 
}

回答by Andrew Dunaway

I know that not everyone is a fan of using the continue keyword, but in cases like this I almost like it because of the readability. You could change the following code to use a continue:

我知道不是每个人都喜欢使用 continue 关键字,但在这种情况下,我几乎喜欢它,因为它具有可读性。您可以更改以下代码以使用继续:

string[] lines = rdr.ReadLine().Split('|');
{
    if (z == 0) continue;   // Skip first line

    sb.AppendLine(";Re");
    sb.AppendLine("@C PAMT " + lines[3]);
    sb.AppendLine("@T " + lines[0]);
    sb.AppendLine("@D @I\" + lines[1]).Replace(@"I:\", "");
    sb.AppendLine(lines[2].Replace(";", "\r\n");
}

回答by Jord?o

using (FileStream fs = new FileStream(filename, FileMode.Open)) 
using (StreamReader rdr = new StreamReader(fs)) { 
  rdr.ReadLine(); // skip the first line
  string line = null;
  while ((line = rdr.ReadLine()) != null) {
    string[] lines = line.Split('|');
    sb.AppendLine(";Re"); 
    sb.AppendLine("@C PAMT " + lines[3]); 
    sb.AppendLine("@T " + lines[0]); 
    sb.AppendLine("@D @I\" + lines[1]).Replace("I:\", ""); 
    sb.AppendLine(lines[2].Replace(";", "\r\n"); 
  }
} 

if (sb.Length == 0) return; // return if nothing was processed

using (FileStream fs = new FileStream(outputfilename, FileMode.Create)) 
using (StreamWriter writer = new StreamWriter(fs)) 
{ 
    writer.Write(sb.ToString()); 
} 

回答by Isti115

If you want to use it more times in your program then it's maybe a good idea to make a custom class inherited from StreamReader with the ability to skip lines.

如果您想在程序中多次使用它,那么制作一个从 StreamReader 继承的自定义类并具有跳过行的能力可能是个好主意。

Something like this could do:

这样的事情可以做:

class SkippableStreamReader : StreamReader
{
    public SkippableStreamReader(string path) : base(path) { }

    public void SkipLines(int linecount)
    {
        for (int i = 0; i < linecount; i++)
        {
            this.ReadLine();
        }
    }
}

after this you could use the SkippableStreamReader's function to skip lines. Example:

在此之后,您可以使用 SkippableStreamReader 的函数来跳过行。例子:

SkippableStreamReader exampleReader = new SkippableStreamReader("file_to_read");

//do stuff
//and when needed
exampleReader.SkipLines(number_of_lines_to_skip);