如何将文本文件拆分成多个文件?

时间:2010-11-30 05:02:40

标签: c# .net string text performance

在C#中,将文本文件拆分为多个文本文件(拆分分隔符为空行)的最有效方法是什么,同时保留字符编码?

3 个答案:

答案 0 :(得分:7)

我会使用StreamReader和StreamWriter类:

 public void Split(string inputfile, string outputfilesformat) {
     int i = 0;
     System.IO.StreamWriter outfile = null;
     string line; 

     try {
          using(var infile = new System.IO.StreamReader(inputfile)) {
               while(!infile.EndOfStream){
                   line = infile.ReadLine();
                   if(string.IsNullOrEmpty(line)) {
                       if(outfile != null) {
                           outfile.Dispose();
                           outfile = null;
                       }
                       continue;
                   }
                   if(outfile == null) {
                       outfile = new System.IO.StreamWriter(
                           string.Format(outputfilesformat, i++),
                           false,
                           infile.CurrentEncoding);
                   }
                   outfile.WriteLine(line);
               }

          }
     } finally {
          if(outfile != null)
               outfile.Dispose();
     }
 }

然后你会这样调用这个方法:

 Split("C:\\somefile.txt", "C:\\output-files-{0}.txt");

答案 1 :(得分:0)

纯粹是为了那些想要避免思考的人:

如果您有一个CSV(逗号分隔值)文件,并希望在字段更改时拆分文件,请通过更改来识别/命名文件(没有不必要的引号),并删除注释/某些行(此处已标识)以“#)

开头

修改方法:

public void Split(string inputfile, string outputfilesformat)
{

    System.IO.StreamWriter outfile = null;
    string line;
    string[] splitArray;
    string nameFromFile = "";
    try
    {
        using (var infile = new System.IO.StreamReader(inputfile))
        {
            while (!infile.EndOfStream)
            {
                line = infile.ReadLine();
                splitArray = line.Split(new char[] { ',' });
                if (!splitArray[0].StartsWith("\"#"))
                {
                    if (splitArray[4].Replace("\"", "") != nameFromFile.Replace("\"", ""))
                    {
                        if (outfile != null)
                        {
                            outfile.Dispose();
                            outfile = null;
                        }
                        nameFromFile = splitArray[4].Replace("\"", "");
                        continue;
                    }
                    if (outfile == null)
                    {
                        outfile = new System.IO.StreamWriter(
                            string.Format(outputfilesformat, nameFromFile),
                            false,
                            infile.CurrentEncoding);
                    }
                    outfile.WriteLine(line);
                }
            }
        }
    }
    finally
    {
        if (outfile != null)
            outfile.Dispose();
    }
}

本地路径呼叫:

    string strpath = Server.MapPath("~/Data/SPLIT/DATA.TXT");
    string newFile = Server.MapPath("~/Data/SPLIT");
    if (System.IO.File.Exists(@strpath))
    {
        Split(strpath, newFile+"\\{0}.CSV");
    }

答案 2 :(得分:-1)

如果任何人都需要使用字符串将文本文件拆分为多个文件:

public static void Main(string[] args)
    {
         void Split(string inputfile, string outputfilesformat)
        {
            int i = 0;
            System.IO.StreamWriter outfile = null;
            string line;

            try
            {
                using (var infile = new System.IO.StreamReader(inputfile))
                {

                    while (!infile.EndOfStream)
                    {
                        line = infile.ReadLine();
                        if (line.Trim().Contains("String You Want File To Split From"))
                        {
                            if (outfile != null)
                            {
                                outfile.Dispose();
                                outfile = null;
                            }
                            continue;
                        }
                        if (outfile == null)
                        {
                            outfile = new System.IO.StreamWriter(
                                string.Format(outputfilesformat, i++),
                                false,
                                infile.CurrentEncoding);
                        }
                        outfile.WriteLine(line);
                    }

                }
            }
            finally
            {
                if (outfile != null)
                    outfile.Dispose();
            }
        }
        Split("C:test.txt", "C:\\output-files-{0}.txt");

    }