读取一个文本文件,直到一个特定的单词,并在一个新的文本文件中复制行

本文关键字:一个 文件 文本 复制 读取 单词 | 更新日期: 2023-09-27 18:13:26

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

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表示组),但是当我找到一个特定的单词时,我想停下来阅读源文本文件:"Betting"。我该怎么做呢?谢谢!:)

编辑:这是一个源文本文件的例子:

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

所以,我想在有"Betting"这个词的行前加几行

读取一个文本文件,直到一个特定的单词,并在一个新的文本文件中复制行

你的问题不太清楚,所以很难知道你到底想在哪里停下来,但像这样的东西可能会工作....

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;
            }
        }
    }
}