8

In the program I am writing, I want to be able to import CSV files. At the moment, it takes a basic text file in.

File mainemails = new File ("mainemails.txt");

I know that for CSV imports using File is probably not the most time-efficient thing to use. What would be the most efficient way to import a CSV file? Would I have to download any new libraries to use an efficient method?

Nick Welki
  • 435
  • 4
  • 8
  • 13
  • http://stackoverflow.com/questions/101100/csv-api-for-java – barsju Apr 03 '12 at 10:11
  • You should use [open CSV](http://opencsv.sourceforge.net/) and if you wish to work with Microsoft Office Tools (like doc, docx, xls, xlsx etc), you can use [Apache POI](http://poi.apache.org/download.html) Java API. – Lion Apr 03 '12 at 10:18

2 Answers2

13

You can use opencsv for importing csv data and then constructing objects from it.

I prefer using libraries so that I can save development time. Also they tend to solve specific problems better.

Jayan
  • 18,003
  • 15
  • 89
  • 143
  • Where would I save the library to? – Nick Welki Apr 03 '12 at 10:16
  • @ Nick Welki : I did not understand the question. You can save the library anywhere in your machine. Please go through SO questions relevant to your IDE. Adding a jar to a project is well discussed. – Jayan Apr 03 '12 at 10:59
5

Try OpenCSV as suggested here and here.

Community
  • 1
  • 1
darrengorman
  • 12,952
  • 2
  • 23
  • 24