C# 如何删除文本文件的第一行?

发布于 2024-12-22 01:16:26 字数 1484 浏览 2 评论 0原文

我想在 Visual Studio 中创建一个 Windows 窗体应用程序,单击按钮即可写入文本文件。

我有一个 txt 文件(例如 test.txt),其中包含

AAAA
BBBB
CCCC
DDDD
EOS
FFFF
GGGG
HHHH
IIII
EOS
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

然后我想将其拆分为许多其他 txt 文件,

**bag1.txt**
AAAA
BBBB
CCCC
DDDD
EOS

**bag2.txt**
EEEE
FFFF
GGGG
IIII
EOS

**bag3.txt**
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

这些是代码,

private void read3btn_Click(object sender, EventArgs e)
    {
        string fileName = textBox1.Text;
        TextReader sr = new StreamReader(fileName);
          //This allows you to do one Read operation.

        string s = sr.ReadToEnd();;
        sr.Close();

        string[] bags = s.Split(new string[] {"EOS"}, StringSplitOptions.None);

        // This will give you an array of strings (minus the EOS field)
        // Then write the files...

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag1.txt", bags[0] + "EOS");  //< -- Add this you need the EOS at the end field the field

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag2.txt", bags[1] + "EOS");

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag3.txt", bags[2] + "EOS" + bags[3]);

   }}

然后输出是这样的

**bag1.txt**
    AAAA
    BBBB
    CCCC
    DDDD
    EOS

**bag2.txt**

EEEE
FFFF
GGGG
IIII
EOS

**bag3.txt**

JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

,不幸的是bags [1]和bags [2]中的输出第一行有一个空行,有办法更新代码吗?

I want to create a Windows Forms app in Visual Studio that writes text files on a button click.

I have a txt file (e.g. test.txt) which contains

AAAA
BBBB
CCCC
DDDD
EOS
FFFF
GGGG
HHHH
IIII
EOS
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

Then I would like to split it into number of other txt files

**bag1.txt**
AAAA
BBBB
CCCC
DDDD
EOS

**bag2.txt**
EEEE
FFFF
GGGG
IIII
EOS

**bag3.txt**
JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

these is the code,

private void read3btn_Click(object sender, EventArgs e)
    {
        string fileName = textBox1.Text;
        TextReader sr = new StreamReader(fileName);
          //This allows you to do one Read operation.

        string s = sr.ReadToEnd();;
        sr.Close();

        string[] bags = s.Split(new string[] {"EOS"}, StringSplitOptions.None);

        // This will give you an array of strings (minus the EOS field)
        // Then write the files...

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag1.txt", bags[0] + "EOS");  //< -- Add this you need the EOS at the end field the field

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag2.txt", bags[1] + "EOS");

        System.IO.File.WriteAllText(@"D:\Program-program\tesfile\bag3.txt", bags[2] + "EOS" + bags[3]);

   }}

Then the output comes this way

**bag1.txt**
    AAAA
    BBBB
    CCCC
    DDDD
    EOS

**bag2.txt**

EEEE
FFFF
GGGG
IIII
EOS

**bag3.txt**

JJJJ
KKKK
LLLL
MMMM
NNNN
EOS
EOF

unfortunately the output in bags[1] and bags[2] has a blank line in the first line, is there anyway to update the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑咖 2024-12-29 01:16:26

您的 "EOS" 分隔符不包含换行符。尝试:

string[] bags = s.Split(new string[] {"EOS\n"}, StringSplitOptions.None);

您的输入文件是

...DDDD\nEOS\nEEEE\n...

在与代码拆分后,您会得到:

...DDDD\n  EOS  \nEEEE\n...

注意 EEEE 之前的前导 \n。通过在分隔符中包含 \n ,您将得到:

...DDDD\n  EOS\n  EEEE\n...

Your "EOS" separator does not contain a newline. Try:

string[] bags = s.Split(new string[] {"EOS\n"}, StringSplitOptions.None);

Your input file is

...DDDD\nEOS\nEEEE\n...

After splitting with your code, you get:

...DDDD\n  EOS  \nEEEE\n...

Notice the leading \n before EEEE. By including \n in your separator, you will get:

...DDDD\n  EOS\n  EEEE\n...
薄情伤 2024-12-29 01:16:26

调用 .Trim() 删除前导空格。

Call .Trim() to remove the leading whitespace.

九歌凝 2024-12-29 01:16:26

好吧,这不是什么大问题:)
当您读取文件时,您会得到类似“AAA\nBBB\nCCC\nDDD\nEOS\nEEE\nFFF\n...EOS\nJJJ\n...”的字符串
如果仅在“EOS”上修剪字符串,您会得到:

“AAA\nBBB\nCCC\nDDD\n”“\nEEE\nFFF\n...”“\nJJJ...”

因为 Split() 方法删除“EOS”字符串,但不删除它后面的新行:)

string[] bags = s.Split(new string[] {"EOS\n"}, StringSplitOptions.None);

..这应该可以正常工作:)

Ok, that's not a big problem :)
When you read your file, you get a string like "AAA\nBBB\nCCC\nDDD\nEOS\nEEE\nFFF\n...EOS\nJJJ\n..."
If tou trim the string only on "EOS", you get this:

"AAA\nBBB\nCCC\nDDD\n" "\nEEE\nFFF\n..." "\nJJJ..."

Because the Split() method removes the "EOS" string, but not the new line it follows :)

string[] bags = s.Split(new string[] {"EOS\n"}, StringSplitOptions.None);

..this should work fine :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文