2017-06-05 5 views
1

Ich möchte einige Tipps und Hilfe zu einem Lesen/Schreiben Teil meiner C#.StreamWriter: Start und Ende auf einer bestimmten Zeilennummer

Situation:

  • Ich habe eine CSV-Datei zu lesen; - OK
  • Wenn der Name der CSV-Datei mit "Load_" beginnt, möchte ich auf eine andere CSV die Daten von Zeile 2 bis zum letzten schreiben;
  • Wenn der Name der CSV-Datei mit "RO_" beginnt, möchte ich auf zwei verschiedene CSVs schreiben, 1 mit den Zeilen 1 bis 4 und die anderen 4 mit dem letzten;

Was ich bisher habe, ist:

public static void ProcessFile(string[] ProcessFile) 
    { 

     // Keeps track of your current position within a record 
     int wCurrLine = 0; 

     // Number of rows in the file that constitute a record 
     const int LINES_PER_ROW = 1; 
     int ctr = 0; 
     foreach (string filename in ProcessFile) 
     { 

      var sbText = new System.Text.StringBuilder(100000); 
      int stop_line = 0; 
      int start_line = 0; 

      // Used for the output name of the file 
      var dir = Path.GetDirectoryName(filename); 
      var fileName = Path.GetFileNameWithoutExtension(filename); 
      var ext = Path.GetExtension(filename); 
      var folderbefore = Path.GetFullPath(Path.Combine(dir, @"..\")); 


      var lineCount = File.ReadAllLines(@filename).Length; 

      string outputname = folderbefore + "output\\" + fileName; 

      using (StreamReader Reader = new StreamReader(@filename)) 
      { 

       if (filename.Contains("RO_")) 
       { 
        start_line = 1; 
        stop_line = 5; 
       } 
       else 
       { 
        start_line = 2; 
        stop_line = lineCount; 
       } 


       ctr = 0; 
       while (!Reader.EndOfStream && ctr < stop_line) 
       { 
        // Add the text 
        sbText.Append(Reader.ReadLine()); 

        // Increment our current record row counter 
        wCurrLine++; 

        // If we have read all of the rows for this record 
        if (wCurrLine == LINES_PER_ROW) 
        { 
         // Add a line to our buffer 
         sbText.AppendLine(); 
         // And reset our record row count 
         wCurrLine = 0; 
        } 
        ctr++; 


       } // end of the while 
      } 

      int total_lenght = sbText.Length 
      // When all of the data has been loaded, write it to the text box in one fell swoop 
      using (StreamWriter Writer = new StreamWriter(dir + "\\" + "output\\" + fileName + "_out" + ext)) 
      { 
       Writer.Write.(sbText.); 
      } 


     } // end of the foreach 

    } // end of ProcessFile 

Ich dachte über die IF/ELSE mit: „Verwendung (Stream Writer = new Stream (dir + "\" + "output \" + Dateiname + "_out" + ext)) "Teil. Ich bin mir jedoch nicht sicher, wie ich an StreamWriter übergeben soll, um nur von/zu einer bestimmten Zeilennummer zu schreiben.

Jede Hilfe ist willkommen! Wenn ich ein paar Informationen verpasse, lass es mich wissen (ich bin ziemlich neu bei stackoverflow).

Vielen Dank.

Antwort

0

-Code ist viel zu kompliziert

using System.Collections.ObjectModel; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 


namespace ConsoleApplication57 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 



     } 
     public static void ProcessFile(string[] ProcessFile) 
     { 
      foreach (string filename in ProcessFile) 
      { 

       // Used for the output name of the file 
       var dir = Path.GetDirectoryName(filename); 
       var fileName = Path.GetFileNameWithoutExtension(filename); 
       var ext = Path.GetExtension(filename); 
       var folderbefore = Path.GetFullPath(Path.Combine(dir, @"..\")); 


       var lineCount = File.ReadAllLines(@filename).Length; 

       string outputname = folderbefore + "output\\" + fileName; 

       using (StreamWriter Writer = new StreamWriter(dir + "\\" + "output\\" + fileName + "_out" + ext)) 
       { 

        int rowCount = 0; 
        using (StreamReader Reader = new StreamReader(@filename)) 
        { 
         rowCount++; 
         string inputLine = ""; 
         while ((inputLine = Reader.ReadLine()) != null) 
         { 

          if (filename.Contains("RO_")) 
          { 
           if (rowCount <= 4) 
           { 
            Writer.WriteLine(inputLine); 
           } 
           if (rowCount == 4) break; 
          } 
          else 
          { 
           if (rowCount >= 2) 
           { 
            Writer.WriteLine(inputLine); 
           } 
          } 

         } // end of the while 
         Writer.Flush(); 
        } 
       } 

      } // end of the foreach 

     } // end of ProcessFile 

    } 




} 
0

Sie LINQ to Take und Skip Linien verwenden können.

public abstract class CsvProcessor 
{ 
    private readonly IEnumerable<string> processFiles; 

    public CsvProcessor(IEnumerable<string> processFiles) 
    { 
     this.processFiles = processFiles; 
    } 

    protected virtual IEnumerable<string> GetAllLinesFromFile(string fileName) 
    { 
     using(var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) 
     using(var reader = new StreamReader()) 
     { 
      var line = String.Empty; 
      while((line = reader.ReadLine()) != null) 
      { 
       yield return line; 
      } 
     } 
    } 

    protected virtual void ProcessFiles() 
    { 
     var sb1 = new StringBuilder(); 
     var sb2 = new StringBuilder(); 

     foreach(var file in this.processFiles) 
     { 
      var fileName = Path.GetFileNameWithoutExtension(file); 
      var lines = GetAllLinesFromFile(file); 

      if(fileName.StartsWith("RO_", StringComparison.InvariantCultureIgnoreCase)) 
      { 
       sb1.AppendLine(lines.Take(4)); //take only the first four lines 
       sb2.AppendLine(lines.Skip(4).TakeWhile(s => !String.IsNullOrEmpty(s))); //skip the first four lines, take everything else 
      } 
      else if(fileName.StartsWith("Load_", StringComparison.InvariantCultureIgnoreCase) 
      { 
       sb2.AppendLine(lines.Skip(1).TakeWhile(s => !String.IsNullOrEmpty(s))); 
      } 
     } 

     // now write your StringBuilder objects to file... 
    } 

    protected virtual void WriteFile(StringBuilder sb1, StringBuilder sb2) 
    { 
     // ... etc.. 
    } 
} 
Verwandte Themen