How to split a text file into multiple files?

前端 未结 3 897
说谎
说谎 2021-01-01 03:30

In C#, what is the most efficient method to split a text file into multiple text files (the splitting delimiter being a blank line), while preserving the character encoding?

3条回答
  •  渐次进展
    2021-01-01 04:20

    I would use the StreamReader and StreamWriter classes:

     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();
         }
     }
    

    You would then call this method like this:

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

提交回复
热议问题