Home > Net >  C # : read TXT file rows and interlaced delete data
C # : read TXT file rows and interlaced delete data

Time:05-13



(in this example the premise condition that every three line TXT file is a set of data, respectively is a blank line, time, target data, the request is to remove the time with a blank line)
Contain space:
 using System; 
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
using System.Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;
using System.IO;


We need to know before operation data in TXT file TXT file number of rows,
 double lines=0; 
Using var (sr=new StreamReader (@ "E: \ nyy TXT"))
{
Var ls="";
While ((ls=sr. ReadLine ())!=null)
{
Lines++;
}
}


Read specify the location of the TXT file after delete specified data generated a new TXT file, the original TXT file is changeless,
In order to prevent the calculation of small and medium-sized trade-off problem (index beyond the scope, the specific situation to analysis) using the try and catch statement, (0, pay attention to the initial behavior and we count 1 row index is 0)
 double b=lines/3;//a total of how many sets of data 
List LineDat=new List (File. ReadAllLines (@ "E: \ nyy TXT"));
Try
{
For (int x=0; X & lt; b; X +=1)
{
LineDat. RemoveAt (1 + x * 3) (int).//the second line from the first delete
}
File. WriteAllLines (@ "E: \ nyy11 TXT", lineDat. ToArray ());//save data
}
Catch
{
File. WriteAllLines (@ "E: \ nyy11 TXT", lineDat. ToArray ());//save data
}

Delete empty line up,

CodePudding user response:

This is a popular science algorithm?


  •  Tags:  
  • C#
  • Related