繁体   English   中英

c#读取文本文件,直到特定单词并在新文本文件中复制行

[英]c# Read a text file until a specific word and copy lines in a new text file

我使用以下代码读取文本文件行并创建一个新文件(正在运行):

StreamReader Read2;

Read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt");
StreamWriter Write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt");


while (!Read2.EndOfStream)
{
    string NewLine = "";
    for (int K = 0; K < 8; K++)
    {
        if (K != 0)
            NewLine = NewLine  + ";" + Read2.ReadLine();
        else
            NewLine = Read2.ReadLine();
    }
    Write2.WriteLine(NewLine);
}

Read2.Close();
Write2.Close();

通过这段代码,我读到文本文件的末尾,并用特定的分隔符(;)放置了几行(每组8条),但是当我发现一个特定的单词时,我想停止阅读源文本文件:投注。我该怎么办?谢谢!:)

编辑:这是源文本文件的示例:

Slask
Termalica
2
0
1.67
3.54
5.11
02.08.2015
Podbeskidzie
Cracovia
0
1
2.88
3.16
2.41
01.08.2015
Wisla
Lech
2
0
3.13
3.18
2.26
01.08.2015
Lechia
Pogon
1
1
1.76
3.45
4.54
31.07.2015
Piast
GornikZ
3
2
2.45
3.18
2.82
31.07.2015
GornikL
GornikZ
2
1
2.45
3.07
2.89
27.07.2015
Jagiellonia
Termalica
2
0
2.04
3.27
3.53
26.07.2015
Legia
Podbeskidzie
5
0
1.29
4.97
9.69
26.07.2015
Pogon
Slask
1
1
2.31
3.19
3.02
26.07.2015
Lech
Lechia
2
1
1.93
3.32
3.82
25.07.2015
Zaglebie
Korona
0
2
1.81
3.33
4.43
25.07.2015
Cracovia
Wisla
1
1
2.19
3.23
3.22
24.07.2015
Ruch
Piast
2
0
2.50
3.09
2.83
24.07.2015
Piast
Termalica
1
0
2.47
3.13
2.83
20.07.2015
Korona
Jagiellonia
3
2
2.31
3.25
2.96
19.07.2015
Slask
Legia
1
4
3.16
3.18
2.23
19.07.2015
Lech
Pogon
1
2
1.49
3.87
6.63
18.07.2015
Ruch
GornikL
0
2
2.25
3.12
3.19
18.07.2015
Zaglebie
Podbeskidzie
1
1
1.87
3.32
4.16
18.07.2015
Lechia
Cracovia
0
1
1.91
3.28
3.98
17.07.2015
Wisla
GornikZ
1
1
1.92
3.35
3.86
17.07.2015
Betting
odds
service&nbsp;provided
in
cooperation
with
OddsPortal.com

这是目标文件的示例:

Slask;Termalica;2;0;1.67;3.54;5.11;02.08.2015
Podbeskidzie;Cracovia;0;1;2.88;3.16;2.41;01.08.2015
Wisla;Lech;2;0;3.13;3.18;2.26;01.08.2015
Lechia;Pogon;1;1;1.76;3.45;4.54;31.07.2015
Piast;GornikZ;3;2;2.45;3.18;2.82;31.07.2015
GornikL;GornikZ;2;1;2.45;3.07;2.89;27.07.2015
Jagiellonia;Termalica;2;0;2.04;3.27;3.53;26.07.2015
Legia;Podbeskidzie;5;0;1.29;4.97;9.69;26.07.2015
Pogon;Slask;1;1;2.31;3.19;3.02;26.07.2015
Lech;Lechia;2;1;1.93;3.32;3.82;25.07.2015
Zaglebie;Korona;0;2;1.81;3.33;4.43;25.07.2015
Cracovia;Wisla;1;1;2.19;3.23;3.22;24.07.2015
Ruch;Piast;2;0;2.50;3.09;2.83;24.07.2015
Piast;Termalica;1;0;2.47;3.13;2.83;20.07.2015
Korona;Jagiellonia;3;2;2.31;3.25;2.96;19.07.2015
Slask;Legia;1;4;3.16;3.18;2.23;19.07.2015
Lech;Pogon;1;2;1.49;3.87;6.63;18.07.2015
Ruch;GornikL;0;2;2.25;3.12;3.19;18.07.2015
Zaglebie;Podbeskidzie;1;1;1.87;3.32;4.16;18.07.2015
Lechia;Cracovia;0;1;1.91;3.28;3.98;17.07.2015
Wisla;GornikZ;1;1;1.92;3.35;3.86;17.07.2015

因此,我要在有“投注”一词的行之前

您的问题不太清楚,因此很难知道您到底想在哪里停止,但是类似的事情可能会起作用。

StreamReader Read2;

    Read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt");
    StreamWriter Write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt");


    while (!Read2.EndOfStream)
    {

        string NewLine = "";
        for (int K = 0; K < 8; K++)
        {
            if (K != 0)
                NewLine = NewLine  + ";" + Read2.ReadLine();
            else
                NewLine = Read2.ReadLine();               
        }

         if(NewLine.Contains("Specific Word"))
                break;

        Write2.WriteLine(NewLine);
    }

    Read2.Close();
    Write2.Close();

我想如果您发现流在八行之前结束,您可能会对这行有问题。

NewLine = NewLine  + ";" + Read2.ReadLine();

我对您的代码进行了一些更改,并根据需要进行测试和调整:

using (StreamReader read2 = File.OpenText("save" + campionatoselezTxt.Text + "bex.txt"))
{
    using (StreamWriter write2 = File.CreateText("read" + campionatoselezTxt.Text + "bex.txt"))
    {
        int k = 0;
        string newLine = string.Empty;

        while (!read2.EndOfStream)
        {
            if (k == 8)
            {
                write2.WriteLine(newLine);
                k = 0;
                newLine = string.Empty;
            }

            newLine = string.IsNullOrEmpty(newLine) ? read2.ReadLine() : newLine + ";" + read2.ReadLine();
            k++;

            if (newLine.Contains("Betting"))
            {
                write2.WriteLine(newLine.Substring(0, newLine.IndexOf("Betting")));
                break;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM