saad
saad

Reputation: 101

read .csv file into 1D array c#

I would like to convert this code from java to C# I need to write line by line from csv and store it in an array?

String csvFile = "data.csv";
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) 
{

  while ((line = br.readLine()) != null) 
  {

 // use comma as separator
 String[] data = line.split(cvsSplitBy);
 System.out.println(Integer.parseInt(data[0]) + "  "+data[1] +  "  "+data[2] );
  }

 } 
catch (IOException e) 
{
 e.printStackTrace();
}

Any suggestions?

Upvotes: 0

Views: 801

Answers (1)

Thangamani Eraniyan
Thangamani Eraniyan

Reputation: 83

If you are trying to parse each record/row into array, this might help.

using (StreamReader sr = new StreamReader("maybyourfilepat\data.csv"))
{
   string line = sr.ReadLine();
   //incase if you want to ignore the header
   while (line != null) 
   {
       string[] strCols = line.Split(',');
       line = sr.ReadLine();
   }
}

Upvotes: 1

Related Questions