0

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?

2
  • 2
    what seems to be the problem..? also I would personally create a class that stores the fields as the same structure as the .csv file and create a List<T> and populate your data that way.. there are literally 1000's of examples online as well as SO that give numerous ways 1 can parse a .csv file please do a google search and edit this question to state what the problem is. Commented Dec 2, 2017 at 0:43
  • Ugh, in Java "this class assumes that the default character encoding". If you actually wanted that in C#, you would pass Encoding.Default to your file I/O constructor. Commented Dec 2, 2017 at 1:00

1 Answer 1

1

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();
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.