C#将大文件分割成一个个小文件
/// <summary>
/// 分割文件
/// </summary>
/// <param name="fileName">原文件路径</param>
/// <param name="outputFileName">目标文件路径</param>
public ActionResult SplitText(string fileName,string outputFileName)
{
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
{
int fileNumber = 0;
while (!sr.EndOfStream)
{
int count = 0;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFileName + ++fileNumber + ".txt"))
{
sw.AutoFlush = true;
while (!sr.EndOfStream && ++count < 20000)
{
sw.WriteLine(sr.ReadLine());
}
}
}
}
}
catch (Exception e)
{
return Content("失败"+e.StackTrace);
}
return Content("成功");
}