我想在Visual Studio中創建一個Windows窗體應用程序,它可以在單擊按鈕上寫入文本文件。C#如何刪除我的文本文件的第一行?
我有一個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
不幸的是袋子[1]和袋子[2]的輸出在第一行有空行,是否有更新代碼?
除非換行符是'\ r'或'\ r \ n' – 2011-12-18 19:01:21
我想它可能是。我將把它留給OP來確定他的具體輸入文本文件。 – 2011-12-18 19:02:48
這是錯誤的,據說索引超出了數組的範圍。 :( – Gamma 2011-12-18 19:04:37