Read Write CSV file in Java. Parse CSV in Java. If you want to work with Comma separated Files CSV in Java, heres a quick API for you. As Java doesnt support parsing of CSV files natively, we have to rely on third party library. Opencsv is one of the best library available for this purpose. Do you want to convert a CSV file to a PDF file Dont download software use Zamzar to convert it for free online. Click to convert your CSV file now. Accurate and uptodate information for all 45,619 U. S. cities including county, state, population, zip codes, coordinates and more. Download files from Google Drive using a computer or an Android device. Go to drive. google. com. Click a file to download. Tuberculosis TB a set of. csv data files for download, including tuberculosis burden estimates, case notifications and treatment outcomes by country. Its open source and is shipped with Apache 2. Lets us see different APIs to parse CSV file. Before that we will need certain tools for this example Tools Technologies. Java JDK 1. 5 or above. Open. CSV library v. Eclipse 3. 2 above optionalLets get started. Reading CSV file in Java. We will use following CSV sample file for this example File sample. COUNTRY,CAPITAL,POPULATION. India,New Delhi, 1. B. Peoples republic of China,Beijing, 1. B. United States,Washington D. C., 0. 3. 1B. Read CSV file line by line String csv. Filename C sample. CSVReader csv. Reader new CSVReadernew File. Readercsv. Filename. String row null. Reader. read. Next null. System. out. printlnrow0. Reader. close. In above code snippet, we use read. Next method of CSVReader class to read CSV file line by line. It returns a String array for each value in row. It is also possible to read full CSV file once. The read. All method of CSVReader class comes handy for this. String row null. String csv. Filename C worksample. CSVReader csv. Reader new CSVReadernew File. Readercsv. Filename. List content csv. Reader. read. All. Object object content. String object. System. out. printlnrow0. Reader. close. The read. All method returns a List of String for given CSV file. Both of the above code snippet prints output Output. COUNTRY CAPITAL POPULATION. India New Delhi 1. B. Peoples republic of China Beijing 1. B. United States Washington D. C. 0. 3. 1B. Use different separator and quote characters. If you want to parse a file with other delimiter like semicolon or hash, you can do so by calling a different constructor of CSVReader class CSVReader reader new CSVReadernew File. Readerfile,. CSVReader reader new CSVReadernew File. Readerfile,. Also if your CSV files value is quoted with single quote instead of default double quote, then you can specify it in constructor CSVReader reader new CSVReadernew File. Readerfile, ,. Also it is possible to skip certain lines from the top of CSV while parsing. You can provide how many lines to skip in CSVReaders constructor. For example the below reader will skip 5 lines from top of CSV and starts processing at line 6. CSVReader reader new CSVReadernew File. Readerfile, 5. Writing CSV file in Java. Creating a CSV file is as simple as reading one. All you have to do is it create the data list and write using CSVWriter class. Below is the code snippet where we write one line in CSV file. String csv C output. CSVWriter writer new CSVWriternew File. Writercsv. String country IndiaChinaUnited States. Nextcountry. writer. We created object of class CSVWriter and called its write. Next method. The write. Next methods takes String as argument. You can also write a List of String to CSV directly. Following is code snippet for that. String csv C output. CSVWriter writer new CSVWriternew File. Writercsv. Listlt String data new Array. Listlt String. String India, New Delhi. String United States, Washington D. C. data. addnew String Germany, Berlin. Alldata. writer. We used write. All method of class CSVWriter to write a List of String as CSV file. Mapping CSV with Java beans. In above examples we saw how to parse CSV file and read the data in it. We retrieved the data as String array. Each record got mapped to String. It is possible to map the result to a Java bean object. For example we created a Java bean to store Country information. Country. java The bean object to store Countries information. Country. private String country. Name. private String capital. String get. Country. Name. return country. Name. public void set. Country. NameString country. Name. this. country. Name country. Name. String get. Capital. CapitalString capital. Now we can map this bean with Opencsv and read the CSV file. Check out below example Column. Position. Mapping. Strategy strat new Column. Position. Mapping. Strategy. strat. TypeCountry. class. String columns new String country. Name, capital the fields to bind do in your Java. Bean. strat. set. Column. Mappingcolumns. Csv. To. Bean csv new Csv. To. Bean. String csv. Filename C sample. CSVReader csv. Reader new CSVReadernew File. Readercsv. Filename. List list csv. parsestrat, csv. Reader. for Object object list. Country country Country object. System. out. printlncountry. Capital. Check how we mapped Country class using Column. Position. Mapping. Strategy. Also the method set. Column. Mapping is used to map individual property of Java bean to the CSV position. In this example we map first CSV value to country. Name attribute and next to capital. Dumping SQL Table as CSVOpen. CSV also provides support to dump data from SQL table directly to CSV. For this we need Result. Set object. Following API can be used to write data to CSV from Result. Set. java. sql. Result. Set my. Result. Set get. Result. Set. From. Somewhere. writer. Allmy. Result. Set, include. Headers. The write. AllResult. Set, boolean method is utilized for this. The first argument is the Result. Set which you want to write to CSV file. And the second argument is boolean which represents whether you want to write header columns table column names to file or not. Download Source Code. Read. WriteCSVJavaexample.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |