C# 如何删除文本文件的第一行?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
"EOS"
分隔符不包含换行符。尝试:您的输入文件是
在与代码拆分后,您会得到:
注意
EEEE
之前的前导\n
。通过在分隔符中包含\n
,您将得到:Your
"EOS"
separator does not contain a newline. Try:Your input file is
After splitting with your code, you get:
Notice the leading
\n
beforeEEEE
. By including\n
in your separator, you will get:调用
.Trim()
删除前导空格。Call
.Trim()
to remove the leading whitespace.好吧,这不是什么大问题:)
当您读取文件时,您会得到类似“AAA\nBBB\nCCC\nDDD\nEOS\nEEE\nFFF\n...EOS\nJJJ\n...”的字符串
如果仅在“EOS”上修剪字符串,您会得到:
“AAA\nBBB\nCCC\nDDD\n”“\nEEE\nFFF\n...”“\nJJJ...”
因为 Split() 方法删除“EOS”字符串,但不删除它后面的新行:)
..这应该可以正常工作:)
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 :)
..this should work fine :)