【发布时间】:2011-09-02 22:20:43
【问题描述】:
如何将 1000 行的单个文本文件拆分为多个较小的文件,例如每个 300 行?请记住,原始文件的行数可能多于或少于一千行。
file1.txt 300 lines -> rest
file2.txt 300 lines -> rest
file3.txt 300 lines -> rest
file4.txt 100 lines
我尝试了以下方法,但它不起作用。
int counter = 0;
string line;
string lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");
System.IO.StreamReader inputfile;
inputfile = new System.IO.StreamReader(new_path);
while ((line = inputfile.ReadLine()) != null)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt", true);
string _replaceBackspace = ReplaceBackspace(read_file.ReadLine().ToLower());
using (StreamWriter writer = new StreamWriter(lineoutput, true))
{
if (counter == 5000)
{
counter = 0;
lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");
}
writer.WriteLine(line.ToLower());
}
counter++;
}
【问题讨论】:
-
这就是我的示例所做的。这就是原因:“if (reader.EndOfStream) break;”
标签: c# file-io streamreader