Split CSV/TXT file

void Main()
{
var path = @"c:\sourceGit\speciesLatLon.txt";
var inputLines = File.ReadAllLines(path);

// Holds all the lines to be added to each output file
var linesForCurrentSpeciesFile = new List<string>();

// Read first row
int i = 0;
var currentSpecies = GetSpecies(inputLines[i]);

// initialize hold value
var holdValue = currentSpecies;

// Initialize output values
linesForCurrentSpeciesFile.Add(inputLines[i]);

// Read next value
i++;

while( i < inputLines.Length )
{
currentSpecies = GetSpecies(inputLines[i]);
if (currentSpecies != holdValue)
{
// output current file
WriteSpeciesFile(holdValue, linesForCurrentSpeciesFile);

// Initialize new output file by clearing out the previous
linesForCurrentSpeciesFile.Clear();

// update hold value with the value just examined.
holdValue = currentSpecies;
}
// Add the current line to the output file
linesForCurrentSpeciesFile.Add(inputLines[i]);
i++;
}
// Write the output file because last row is equal to a break in the sequence
WriteSpeciesFile(currentSpecies, linesForCurrentSpeciesFile);
}

// Define other methods and classes here
public string GetSpecies(string line)
{
// return the first value of the input line
return line.Split(new char[] {','})[0];
}

public void WriteSpeciesFile(string species, List<string> content)
{
File.WriteAllLines(string.Format(@"C:\sourceGit\{0}.csv", species), content.ToArray());
}

上一篇:Layout Resource官方教程(2)用ViewStub引用的嵌入的layout可推迟加载


下一篇:[转]关于Android系统的”点九”