CINXE.COM
Jean Fernando - Stack Abuse
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Jean Fernando - Stack Abuse</title><link rel="canonical" href="https://stackabuse.com/author/jean/"/><meta property="og:url" content="https://stackabuse.com/author/jean/"/><meta name="twitter:url" content="https://stackabuse.com/author/jean/"/><link rel="next" href="https://stackabuse.com/author/jean/page/2/"/><meta property="og:site_name" content="Stack Abuse"/><meta property="og:type" content="profile"/><meta property="og:title" content="Jean Fernando - Stack Abuse"/><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Jean Fernando - Stack Abuse"/><meta name="twitter:site" content="@StackAbuse"/><script type="application/ld+json">{ "@context": "https://schema.org", "@type": "Person", "sameAs": [], "name": "Jean Fernando", "url": "https://stackabuse.com/author/jean/", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://stackabuse.com/" }, "image": { "@type": "ImageObject", "url": "/assets/images/logo-sa-small.png" } }</script><meta name="next-head-count" content="14"/><link href="//fonts.googleapis.com" rel="preconnect"/><link href="//fonts.gstatic.com" rel="preconnect" crossorigin="true"/><link href="//cdnjs.cloudflare.com" rel="preconnect"/><link href="//s3.stackabuse.com" rel="preconnect"/><link href="//googletagmanager.com" rel="dns-prefetch"/><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="HandheldFriendly" content="True"/><link rel="shortcut icon" sizes="any" href="/assets/images/favicon.ico"/><link rel="shortcut icon" type="image/svg+xml" href="/assets/images/favicon.svg"/><link rel="preload" as="style" href="//fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap"/><link rel="preload" as="style" href="//fonts.googleapis.com/css?family=Nunito:400,400i,700,700i&display=swap"/><meta name="referrer" content="no-referrer-when-downgrade"/><link rel="alternate" type="application/rss+xml" title="Stack Abuse" href="https://stackabuse.com/rss/"/><link rel="preload" href="/_next/static/css/eaee8aa5ba78cb00.css" as="style"/><link rel="stylesheet" href="/_next/static/css/eaee8aa5ba78cb00.css" data-n-g=""/><noscript data-n-css=""></noscript><script defer="" nomodule="" src="/_next/static/chunks/polyfills-c67a75d1b6f99dc8.js"></script><script src="/_next/static/chunks/webpack-a2e6e06dd4ae8a03.js" defer=""></script><script src="/_next/static/chunks/framework-1dabaad0401a053e.js" defer=""></script><script src="/_next/static/chunks/main-1b53524783caa372.js" defer=""></script><script src="/_next/static/chunks/pages/_app-85dd70c54f224ed0.js" defer=""></script><script src="/_next/static/chunks/c78d26b1-fe043233b220ebfb.js" defer=""></script><script src="/_next/static/chunks/816-dfcc5ef3cff7cadb.js" defer=""></script><script src="/_next/static/chunks/4835-537702d13aff89de.js" defer=""></script><script src="/_next/static/chunks/7329-ea47eb245049fa3f.js" defer=""></script><script src="/_next/static/chunks/2221-aa293f4e0cf5d5c8.js" defer=""></script><script src="/_next/static/chunks/pages/author/%5B...slug%5D-b07045ad2b81ff6e.js" defer=""></script><script src="/_next/static/1740324887682/_buildManifest.js" defer=""></script><script src="/_next/static/1740324887682/_ssgManifest.js" defer=""></script></head><body><div id="__next"><div><nav class="bg-white pb-2 border-b shadow"><div class="mx-8 px-2 sm:px-4 lg:px-8"><div class="flex justify-between h-16"><div class="flex items-center px-2 lg:px-0"><a href="/"><div class="flex-shrink-0 flex items-center"><svg class="h-6 w-full" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 494.36 63"><defs><style>.cls-1{fill:#363636}.cls-2{fill:#f29c80}.cls-3{fill:#b34a28}.cls-4{fill:#f16335}</style></defs><title>SA Logotype</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M104.94,27.65q-3.15-1.44-4.65-2.16c-1-.48-2.17-1.12-3.49-1.91A10.35,10.35,0,0,1,94,21.35a3.58,3.58,0,0,1-.82-2.27,4.79,4.79,0,0,1,1.74-3.89,7.6,7.6,0,0,1,5-1.44,16,16,0,0,1,5.61,1.15,27.13,27.13,0,0,1,4.1,1.84c.73.46,1.3.83,1.71,1.12l5.2-10.8-2-1.44A28.63,28.63,0,0,0,108,2.84a29.37,29.37,0,0,0-9-1.4A18.5,18.5,0,0,0,86.2,6.52a16.81,16.81,0,0,0-5.61,13.06,14.64,14.64,0,0,0,2.81,8.61,21.59,21.59,0,0,0,7.93,6.66c.82.43,2.09,1.07,3.8,1.91s2.94,1.46,3.69,1.87a27.75,27.75,0,0,1,2.6,1.65,6.9,6.9,0,0,1,2,2.09,4.57,4.57,0,0,1,.58,2.27q0,5.84-8.14,5.83a14.52,14.52,0,0,1-6.46-1.87,35.41,35.41,0,0,1-5.1-3c-.95-.74-1.71-1.35-2.25-1.83L75.67,55.15l.89.79a19.23,19.23,0,0,0,2.8,2c1.28.77,2.71,1.55,4.31,2.34a26.52,26.52,0,0,0,6,2,33.14,33.14,0,0,0,7.25.79,29.54,29.54,0,0,0,8-1,21.94,21.94,0,0,0,6.74-3.21,14.53,14.53,0,0,0,4.79-6,21.36,21.36,0,0,0,1.74-8.93,13.77,13.77,0,0,0-3.86-9.54A28,28,0,0,0,104.94,27.65Z"></path><path class="cls-1" d="M143.66,50.69a5.8,5.8,0,0,1-1.17.57,3.84,3.84,0,0,1-1.43.29q-2.75,0-2.74-5.11V34.7h7.39V25h-7.39V11.23H126.21V25h-5.06V34.7h5.06V48.89a22.21,22.21,0,0,0,.55,5,16,16,0,0,0,1.81,4.47,9,9,0,0,0,3.73,3.38A12.76,12.76,0,0,0,138.18,63a17.23,17.23,0,0,0,8.9-2.3l-3-10.3Z"></path><path class="cls-1" d="M177.44,28.51A15.41,15.41,0,0,0,166,23.9a15.82,15.82,0,0,0-12.51,5.69,20.47,20.47,0,0,0-4.93,13.9,20.33,20.33,0,0,0,4.93,13.86A15.85,15.85,0,0,0,166,63a14.47,14.47,0,0,0,11.49-5.26h.14v4.18h12V25H177.44Zm-2,21.17a7.68,7.68,0,0,1-5.85,2.45,7.79,7.79,0,0,1-6-2.41,8.74,8.74,0,0,1-2.26-6.23,8.69,8.69,0,0,1,2.36-6.27,8.15,8.15,0,0,1,11.63,0,8.69,8.69,0,0,1,2.36,6.27A8.74,8.74,0,0,1,175.43,49.68Z"></path><path class="cls-1" d="M216.43,34.34a9.4,9.4,0,0,1,5.88,1.73l4.11-8.71-1.16-.86a15,15,0,0,0-4.18-1.73,22.57,22.57,0,0,0-6.43-.87,18.72,18.72,0,0,0-13.57,5.8,18.81,18.81,0,0,0-5.85,13.86,18.52,18.52,0,0,0,5.85,13.72A18.83,18.83,0,0,0,214.65,63a22,22,0,0,0,6.54-.94,20,20,0,0,0,3.89-1.47c.57-.36,1-.66,1.34-.9l-4.11-8.86-.61.43a9.12,9.12,0,0,1-2.16.87,10.87,10.87,0,0,1-3.11.43,7.75,7.75,0,0,1-5.88-2.66,9.22,9.22,0,0,1-2.46-6.48A9.1,9.1,0,0,1,210.55,37,7.72,7.72,0,0,1,216.43,34.34Z"></path><polygon class="cls-1" points="271.43 24.98 255.69 24.98 243.45 37.87 243.45 0 231.34 0 231.34 61.92 243.45 61.92 243.45 47.16 257.47 61.92 273.07 61.92 254.74 42.62 271.43 24.98"></polygon><polygon class="cls-1" points="296.96 2.52 274.18 61.92 288 61.92 302.84 20.88 302.98 20.88 317.82 61.92 331.63 61.92 308.86 2.52 296.96 2.52"></polygon><path class="cls-1" d="M357.42,23.9a15.41,15.41,0,0,0-11.49,4.61V0H333.82V61.92h12V57.74h.14A14.47,14.47,0,0,0,357.42,63a15.88,15.88,0,0,0,12.52-5.65,20.38,20.38,0,0,0,4.92-13.86,20.51,20.51,0,0,0-4.92-13.9A15.86,15.86,0,0,0,357.42,23.9Zm2.33,25.82a7.8,7.8,0,0,1-5.95,2.41A7.65,7.65,0,0,1,348,49.68a8.69,8.69,0,0,1-2.29-6.19A8.64,8.64,0,0,1,348,37.22a7.7,7.7,0,0,1,5.78-2.44,7.78,7.78,0,0,1,5.84,2.44A8.64,8.64,0,0,1,362,43.49,8.78,8.78,0,0,1,359.75,49.72Z"></path><path class="cls-1" d="M405.78,42.55c0,3.65-.53,6.24-1.57,7.78s-2.83,2.3-5.34,2.3-4.29-.77-5.33-2.3S392,46.2,392,42.55V25h-12.1V45.65q0,9.21,4.54,13.28T398.87,63q9.92,0,14.47-4.07t4.55-13.28V25H405.78Z"></path><path class="cls-1" d="M449.11,42.19A20.21,20.21,0,0,0,445.79,40a23.31,23.31,0,0,0-3.69-1.44,8.7,8.7,0,0,1-1-.29c-.44-.14-.74-.25-.93-.32l-.82-.32-.78-.33-.65-.32a2.25,2.25,0,0,1-.58-.4c-.12-.12-.24-.26-.38-.43a1.25,1.25,0,0,1-.27-.54,2.25,2.25,0,0,1-.07-.58,1.82,1.82,0,0,1,.85-1.76,6.53,6.53,0,0,1,2.91-.47,16,16,0,0,1,7.8,2.31l4-8.36-1.43-.72a22.21,22.21,0,0,0-4.38-1.44,24.9,24.9,0,0,0-5.95-.72q-7,0-11.18,3.14a10.32,10.32,0,0,0-4.14,8.74,9.07,9.07,0,0,0,1.91,6,18.12,18.12,0,0,0,5.2,4.06,28.82,28.82,0,0,0,3.59,1.59A14.8,14.8,0,0,1,439,49a2.2,2.2,0,0,1,1,1.9c0,1.78-1.26,2.67-3.76,2.67a9.22,9.22,0,0,1-4-1.19A23.33,23.33,0,0,1,429,50.51c-.6-.46-1.08-.85-1.44-1.19L422.61,58l1.5,1.3A17.63,17.63,0,0,0,429,61.78,21,21,0,0,0,436.15,63q16.62,0,16.62-12.6a11.23,11.23,0,0,0-1-4.75A9.24,9.24,0,0,0,449.11,42.19Z"></path><path class="cls-1" d="M494.36,43.49q0-9.43-5.24-14.51T475.55,23.9a18.46,18.46,0,0,0-13.62,5.76,19,19,0,0,0-5.67,13.83,18.9,18.9,0,0,0,5.67,13.79A18.54,18.54,0,0,0,475.55,63q12.72,0,18.19-9.65L485,48.74a13.63,13.63,0,0,1-3.9,3.28,10.84,10.84,0,0,1-4.65.83,8.3,8.3,0,0,1-5.2-1.87q-2.4-1.87-2.4-5.76h25.45A14.91,14.91,0,0,0,494.36,43.49Zm-25.17-5.33a6,6,0,0,1,1.91-4.32,6.77,6.77,0,0,1,4.79-1.66,6.51,6.51,0,0,1,4.48,1.55,7,7,0,0,1,2.15,4.43Z"></path><polygon class="cls-2" points="66.05 19.08 73.15 1.3 57.25 1.3 50.16 19.08 66.05 19.08"></polygon><polygon class="cls-3" points="40.21 43.99 32.99 62.06 48.88 62.06 56.1 43.99 40.21 43.99"></polygon><polygon class="cls-4" points="57.69 40.01 64.46 23.05 48.57 23.05 41.8 40.01 57.69 40.01"></polygon><polygon class="cls-2" points="33.06 19.08 40.16 1.3 24.27 1.3 17.17 19.08 33.06 19.08"></polygon><polygon class="cls-3" points="7.22 43.99 0 62.06 15.89 62.06 23.11 43.99 7.22 43.99"></polygon><polygon class="cls-4" points="24.7 40.01 31.47 23.05 15.58 23.05 8.81 40.01 24.7 40.01"></polygon></g></g></svg></div></a><div class="hidden lg:block lg:ml-6"><div class="hidden lg:ml-6 lg:flex"><a href="/tools/" class="mr-8 inline-flex items-center px-1 pt-1 border-b-2 border-blue-400 text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700 transition duration-150 ease-in-out">Tools</a><div class="px-1 pt-1 border-b-2 border-green-400 focus:border-gray-300 transition duration-150 ease-in-out"><div class="relative" data-headlessui-state=""><button class="text-gray-500 group rounded-md inline-flex items-center focus:outline-none text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700" type="button" aria-expanded="false" data-headlessui-state=""><span>Learn</span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="text-gray-400 ml-2 h-5 w-5 group-hover:text-gray-500"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg></button></div><div hidden="" style="position:fixed;top:1px;left:1px;width:1px;height:0;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0;display:none"></div></div><a href="/about/" class="ml-8 inline-flex items-center px-1 pt-1 border-b-2 border-red-400 text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700 transition duration-150 ease-in-out">About</a></div></div></div><div class="hidden mt-4 md:flex md:flex-1 md:items-center md:justify-center md:px-2 lg:ml-6 lg:justify-end"><div class="relative w-1/2 text-gray-400 transition duration-200 focus-within:text-gray-600"><div class="absolute inset-y-0 left-0 flex items-center pointer-events-none" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="h-5 w-5 ml-2"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg></div><input id="search-field" class="block w-full h-full pl-10 pr-3 py-3 text-sm border-gray-300 text-gray-900 placeholder-gray-500 rounded transition duration-200 focus:outline-none focus:ring-0 focus:border-gray-400" placeholder="Search" type="text"/></div><div class="hidden ml-6 md:flex items-center"><button class="whitespace-nowrap text-base font-medium text-gray-500 hover:text-gray-900">Sign in</button><button class="ml-6 whitespace-nowrap inline-flex items-center justify-center px-4 py-2 border border-transparent rounded-md shadow-sm text-base font-medium text-white bg-orange-500 hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500">Sign up</button></div></div><div class="flex items-center lg:hidden"><button class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out" aria-label="Main menu" aria-expanded="false"><svg class="block h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg><svg class="hidden h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button></div></div></div><div class="hidden lg:hidden"><div class="pt-2 pb-3"><a href="/tag/python" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Python</a><a href="/tag/javascript" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">JavaScript</a><a href="/tag/java" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Java</a></div></div></nav><main><div class="flex items-stretch min-h-screen"><div class="mx-auto max-w-none lg:max-w-screen-xl px-4 sm:px-6 pb-6"><div class="grid grid-cols-12 sm:gap-x-2 lg:gap-x-3 pt-6"><div class="col-span-12 lg:col-span-3"><img class="object-cover shadow-lg rounded-lg h-60 w-60" src="/assets/images/logo-sa-small.png" alt="Jean Fernando"/><div class="inline-flex py-4"></div></div><div class="col-span-12 lg:col-span-7"><h1 class="my-1 text-3xl tracking-tight leading-10 font-extrabold text-gray-900 sm:leading-none sm:text-6xl lg:text-4xl xl:text-5xl">Jean Fernando</h1><div class="">Articles: <!-- -->4</div><div class="">Joined: <!-- -->Dec 09, 2018</div><div class="my-4"></div></div><div class="lg:col-span-1"></div></div><div class="mt-8 border-t border-gray-200"><h2 class="my-6 text-xl tracking-tight leading-10 font-extrabold text-gray-700 sm:leading-none sm:text-4xl lg:text-2xl xl:text-3xl">Recently published</h2><div class="grid gap-5 lg:grid-cols-3"><div><div class="flex flex-col rounded-lg shadow-lg overflow-hidden"><div class="flex-shrink-0"><a href="/reading-and-writing-csvs-in-java-with-opencsv/"><img class="h-48 w-full object-cover" src="/assets/images/logo-java.png" alt=""/></a></div><div class="flex-1 bg-white p-6 flex flex-col justify-between"><div class="flex-1"><p class="text-sm leading-5 font-medium text-primary">Article</p><a class="block hover:no-underline" href="/reading-and-writing-csvs-in-java-with-opencsv/"><h3 class="mt-2 text-xl leading-7 font-semibold text-gray-900 hover:underline">Reading and Writing CSVs in Java with OpenCSV</h3><p class="mt-3 text-base leading-6 text-gray-500 break-words">This is the final article in a short series dedicated to Libraries for Reading and Writing CSVs in Java, and a direct continuation from the previous article - Reading and Writing CSVs in Java with Apache Commons CSV. OpenCSV OpenCSV is one of the simplest and easiest CSV parsers to...</p></a></div><div class="mt-6 flex items-center"><div class="flex-shrink-0"><a href="/author/jean/"><img class="h-10 w-10 rounded-full" src="/assets/images/logo-sa-small.png" alt="Jean Fernando"/></a></div><div class="ml-3"><p class="text-sm leading-5 font-medium text-gray-900"><a href="/author/jean/" class="hover:underline">Jean Fernando</a></p><div class="flex text-sm leading-5 text-gray-500"><time dateTime="2019-02-20">Feb 20, 2019</time><span class="mx-1">路</span><span>7<!-- --> min read</span></div></div></div></div></div></div><div><div class="flex flex-col rounded-lg shadow-lg overflow-hidden"><div class="flex-shrink-0"><a href="/reading-and-writing-csvs-in-java-with-apache-commons-csv/"><div class="lazyload-wrapper "><div style="height:12rem" class="lazyload-placeholder"></div></div></a></div><div class="flex-1 bg-white p-6 flex flex-col justify-between"><div class="flex-1"><p class="text-sm leading-5 font-medium text-primary">Article</p><a class="block hover:no-underline" href="/reading-and-writing-csvs-in-java-with-apache-commons-csv/"><h3 class="mt-2 text-xl leading-7 font-semibold text-gray-900 hover:underline">Reading and Writing CSVs in Java with Apache Commons CSV</h3><p class="mt-3 text-base leading-6 text-gray-500 break-words">This is the second article in a short series dedicated to Libraries for Reading and Writing CSVs in Java, and a direct continuation from the previous article - Reading and Writing CSVs in Core Java. Apache Commons CSV The Apache Commons CSV library is the Apache Software Foundation's version of...</p></a></div><div class="mt-6 flex items-center"><div class="flex-shrink-0"><a href="/author/jean/"><div class="lazyload-wrapper "><div style="height:2.5rem" class="lazyload-placeholder"></div></div></a></div><div class="ml-3"><p class="text-sm leading-5 font-medium text-gray-900"><a href="/author/jean/" class="hover:underline">Jean Fernando</a></p><div class="flex text-sm leading-5 text-gray-500"><time dateTime="2019-02-19">Feb 19, 2019</time><span class="mx-1">路</span><span>7<!-- --> min read</span></div></div></div></div></div></div><div><div class="flex flex-col rounded-lg shadow-lg overflow-hidden"><div class="flex-shrink-0"><a href="/libraries-for-reading-and-writing-csvs-in-java/"><div class="lazyload-wrapper "><div style="height:12rem" class="lazyload-placeholder"></div></div></a></div><div class="flex-1 bg-white p-6 flex flex-col justify-between"><div class="flex-1"><p class="text-sm leading-5 font-medium text-primary">Article</p><a class="block hover:no-underline" href="/libraries-for-reading-and-writing-csvs-in-java/"><h3 class="mt-2 text-xl leading-7 font-semibold text-gray-900 hover:underline">Libraries for Reading and Writing CSVs in Java</h3><p class="mt-3 text-base leading-6 text-gray-500 break-words">CSV stands for Comma Separated Values, a method of formatting data which has been used even before the use of personal computers became widespread. The format gained popularity because the first computers used punched cards to process data, and comma separated values are easier to 'punch' in than traditional table-formatted...</p></a></div><div class="mt-6 flex items-center"><div class="flex-shrink-0"><a href="/author/jean/"><div class="lazyload-wrapper "><div style="height:2.5rem" class="lazyload-placeholder"></div></div></a></div><div class="ml-3"><p class="text-sm leading-5 font-medium text-gray-900"><a href="/author/jean/" class="hover:underline">Jean Fernando</a></p><div class="flex text-sm leading-5 text-gray-500"><time dateTime="2019-02-18">Feb 18, 2019</time><span class="mx-1">路</span><span>9<!-- --> min read</span></div></div></div></div></div></div><div><div class="flex flex-col rounded-lg shadow-lg overflow-hidden"><div class="flex-shrink-0"><a href="/reading-and-writing-csvs-in-java/"><div class="lazyload-wrapper "><div style="height:12rem" class="lazyload-placeholder"></div></div></a></div><div class="flex-1 bg-white p-6 flex flex-col justify-between"><div class="flex-1"><p class="text-sm leading-5 font-medium text-primary">Article</p><a class="block hover:no-underline" href="/reading-and-writing-csvs-in-java/"><h3 class="mt-2 text-xl leading-7 font-semibold text-gray-900 hover:underline">Reading and Writing CSVs in Java</h3><p class="mt-3 text-base leading-6 text-gray-500 break-words">This is the first article in a short series dedicated to Libraries for Reading and Writing CSVs in Java. Reading and Writing CSVs in Core Java Owning to the popularity and widespread use of CSV as a format for data transfer, there are many parser libraries that can be used...</p></a></div><div class="mt-6 flex items-center"><div class="flex-shrink-0"><a href="/author/jean/"><div class="lazyload-wrapper "><div style="height:2.5rem" class="lazyload-placeholder"></div></div></a></div><div class="ml-3"><p class="text-sm leading-5 font-medium text-gray-900"><a href="/author/jean/" class="hover:underline">Jean Fernando</a></p><div class="flex text-sm leading-5 text-gray-500"><time dateTime="2019-02-18">Feb 18, 2019</time><span class="mx-1">路</span><span>4<!-- --> min read</span></div></div></div></div></div></div></div><div class="mt-8 border-t border-gray-200 px-4 flex items-center justify-between sm:px-0"></div></div></div></div></main><div class="bg-white border-t border-gray-300"><div class="max-w-screen-xl mx-auto pt-12 pb-6 px-4 sm:px-6 md:flex md:items-center md:justify-between lg:px-8"><div class="flex justify-center md:order-2"><a href="https://twitter.com/stackabuse" target="_blank" rel="noreferrer noopener" class="text-gray-500 hover:text-twitter"><span class="sr-only">Twitter</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84"></path></svg></a><a href="https://github.com/stackabuse" target="_blank" rel="noreferrer noopener" class="ml-6 text-gray-500 hover:text-github"><span class="sr-only">GitHub</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd"></path></svg></a><a href="https://www.facebook.com/stackabuse" target="_blank" rel="noreferrer noopener" class="ml-6 text-gray-500 hover:text-facebook"><span class="sr-only">Facebook</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12c0 4.991 3.657 9.128 8.438 9.878v-6.987h-2.54V12h2.54V9.797c0-2.506 1.492-3.89 3.777-3.89 1.094 0 2.238.195 2.238.195v2.46h-1.26c-1.243 0-1.63.771-1.63 1.562V12h2.773l-.443 2.89h-2.33v6.988C18.343 21.128 22 16.991 22 12z" clip-rule="evenodd"></path></svg></a></div><div class="mt-8 md:mt-0 md:order-1"><p class="text-center text-base leading-6 text-gray-500">漏 2013-<!-- -->2025<!-- --> Stack Abuse. All rights reserved.</p><div class="text-sm"><a href="/about" class="pr-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">About</a><a href="/disclosure" class="px-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">Disclosure</a><a href="/privacy-policy" class="px-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">Privacy</a><a href="/terms-of-service" class="pl-4 text-gray-500 hover:no-underline hover:text-gray-600">Terms</a><div id="ccpa" style="display:none;cursor:pointer">Do not share my Personal Information.</div></div></div></div></div></div></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"author":{"id":25,"name":"Jean Fernando","slug":"jean","role_id":3,"img_profile":"/assets/images/logo-sa-small.png","img_cover":null,"bio_html":"","website":null,"location":"Sri Lanka","facebook":null,"twitter":null,"github":null,"created_at":1544358993000,"updated_at":1629840515113,"role":"contributor","contents":[{"id":338,"title":"Reading and Writing CSVs in Java with OpenCSV","slug":"reading-and-writing-csvs-in-java-with-opencsv","body_html":"\u003ch2 id=\"introduction\"\u003eIntroduction\u003c/h2\u003e\n\u003cp\u003eThis is the final article in a short series dedicated to \u003ca target=\"_blank\" href=\"/libraries-for-reading-and-writing-csvs-in-java\"\u003eLibraries for Reading and Writing CSVs in Java\u003c/a\u003e, and a direct continuation from the previous article - \u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-apache-commons-csv\"\u003eReading and Writing CSVs in Java with Apache Commons CSV\u003c/a\u003e.\u003c/p\u003e\n\u003ch2 id=\"opencsv\"\u003eOpenCSV\u003c/h2\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://opencsv.sourceforge.net/\"\u003eOpenCSV\u003c/a\u003e is one of the simplest and easiest CSV parsers to understand, using standard \u003ccode\u003eReader\u003c/code\u003e/\u003ccode\u003eWriter\u003c/code\u003e classes and offering a \u003ccode\u003eCSVReader\u003c/code\u003e implementation on top.\u003c/p\u003e\n\u003cp\u003eJust like Apache Commons CSV, \u003cem\u003eOpenCSV\u003c/em\u003e operates with an Apache 2.0 license. Before downloading and deciding whether to use OpenCSVs parsers, you can browse through the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://sourceforge.net/p/opencsv/source/ci/master/tree/\"\u003esource code and Java docs\u003c/a\u003e, and even check out their JUnit test suite, which is included in their git repository.\u003c/p\u003e\n\u003cp\u003eOpenCSV is also included in the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://mvnrepository.com/artifact/com.opencsv/opencsv/4.4\"\u003eMVNRepository\u003c/a\u003e, making dependency management straightforward.\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003eCSVReader\u003c/code\u003e allows fetching a single record at a time, multiple records as a list or as an iterator, making it flexible in terms of usability of the read data. The library also includes handy features such as reading, writing to and from beans, and direct mapping from a CSV to a Java Map using the header row.\u003c/p\u003e\n\u003cp\u003eOpenCSV does not have as wide a variety of pre-defined formats as Apache Commons CSV. It relies on two parsers:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003cstrong\u003eCSVParser\u003c/strong\u003e - The original parser defined in OpenCSV. This works for most simple parsing instances but fails if there are escape characters defined as part of the record itself.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eRFC4180Parser\u003c/strong\u003e - similar to the \u003ccode\u003eCSVFormat.RFC4180\u003c/code\u003e parser in Apache Commons CSV. Works on CSV files which are formatted according to the specifications of RFC 4180. This version of the parser considers all characters between the opening and closing quotation marks as content, except for the double quote character, which needs to be escaped with another double quote.\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch3 id=\"readingcsvswithopencsv\"\u003eReading CSVs with OpenCSV\u003c/h3\u003e\n\u003cp\u003eReading CSVs with OpenCSV is faster than with Apache Commons CSV because the \u003ccode\u003eCSVWriter\u003c/code\u003e is implemented to be multi-threaded, when using the \u003ccode\u003eCSVToBean.parse()\u003c/code\u003e method.\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003eCSVReader\u003c/code\u003e is also implemented using \u003cem\u003eJava Iterable\u003c/em\u003e, so it is possible to manage both memory and time constraints based on the implementation method you choose.\u003c/p\u003e\n\u003cp\u003eOpenCSV has two objects types for reading CSVs - \u003cstrong\u003eCSVReader\u003c/strong\u003e, and its sub class \u003cstrong\u003eCSVReaderHeaderAware\u003c/strong\u003e.\u003c/p\u003e\n\u003cp\u003e\u003ccode\u003eCSVReader\u003c/code\u003e is similar to its Apache Commons CSV \u003ccode\u003eCSVParser\u003c/code\u003e counterpart and can be used for both simple and complicated parsing scenarios.\u003c/p\u003e\n\u003cp\u003eTo iterate through each record in a CSV file, where \u003ccode\u003erecord\u003c/code\u003e will be a string array with the comma separated values split into individual fields:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVReader csvReader = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVReader (\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e InputStreamReader(csvFile.getInputStream()));\n\u003cspan class=\"hljs-keyword\"\u003ewhile\u003c/span\u003e ((record = csvReader.readNext()) != \u003cspan class=\"hljs-keyword\"\u003enull\u003c/span\u003e) {\n \u003cspan class=\"hljs-comment\"\u003e// do something\u003c/span\u003e\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf your CSV is delimited by a character other than a comma, you can use the two-parameter constructor instead, and specify the delimiter you want the \u003ccode\u003eCSVReader\u003c/code\u003e to use.\u003c/p\u003e\n\u003cp\u003eFor example if your CSV contains tab separated values, you can initialize the \u003ccode\u003eCSVReader\u003c/code\u003e as follows:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVReader csvReader = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVReader(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e InputStreamReader(csvFile.getInputStream()), \u003cspan class=\"hljs-string\"\u003e\u0026#x27;\\t\u0026#x27;\u003c/span\u003e);\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eOpenCSV also has a more complicated way of parsing CSV files which involves implementing beans to map the fields in a CSV, and then use annotations for identifying the types of records with either header-based, or position-based annotations.\u003c/p\u003e\n\u003cp\u003eThis helps because it allows the records of a CSV to be processed as a common dataset, instead of as a collection of individual fields.\u003c/p\u003e\n\u003cp\u003eIf the header names of the file being processed are consistent, you can annotate the columns using the \u003ccode\u003e@CSVBindByName\u003c/code\u003e annotation and allow OpenCSV to take care of the mapping and copying side of processing the parsed data.\u003c/p\u003e\n\u003cp\u003eFor example with our tree dataset:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-class\"\u003e\u003cspan class=\"hljs-keyword\"\u003eclass\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003eTrees\u003c/span\u003e \u003c/span\u003e{\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByName\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e index;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByName\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e girth;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByName\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e height;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByName\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e volume;\n\n \u003cspan class=\"hljs-function\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003egetIndex\u003c/span\u003e\u003cspan class=\"hljs-params\"\u003e()\u003c/span\u003e \u003c/span\u003e{\n \u003cspan class=\"hljs-keyword\"\u003ereturn\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003ethis\u003c/span\u003e.index;\n }\n\n \u003cspan class=\"hljs-function\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003evoid\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003esetIndex\u003c/span\u003e\u003cspan class=\"hljs-params\"\u003e(\u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e newIndex)\u003c/span\u003e \u003c/span\u003e{\n \u003cspan class=\"hljs-keyword\"\u003ethis\u003c/span\u003e.index = newIndex;\n }\n ...\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eAs long as your CSV file contains a header named with the variable names in our class declaration, OpenCSV can parse and read data into the corresponding element, with type conversions automatically handled:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eList\u0026lt;Trees\u0026gt; treeParser = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVToBeanBuilder(FileReader(\u003cspan class=\"hljs-string\"\u003e\u0026quot;somefile.csv\u0026quot;\u003c/span\u003e)).withType(Trees.class).build().parse();\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eValidations can be added to the getter and setter methods where needed, and mandatory fields can be specified by setting the \u003ccode\u003erequired\u003c/code\u003e flag on the annotation.\u003c/p\u003e\n\u003cp\u003eIf the header name is slightly different from the name of the variable, the String can be set in the annotation as well. The ability to map the header name when the column name is different is useful in our example since our actual dataset contains the unit of measure of the field, along with a space and punctuation characters which are not allowed in standard Java variable names.\u003c/p\u003e\n\u003cp\u003eThe flag and mapping can be specified with the annotation in this case:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e...\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByName\u003c/span\u003e (column = \u003cspan class=\"hljs-string\"\u003e\u0026quot;Girth (in)\u0026quot;\u003c/span\u003e, required = \u003cspan class=\"hljs-keyword\"\u003etrue\u003c/span\u003e)\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e girth;\n...\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf your CSV file does not have a header, you can map by column position along with the \u003ccode\u003e@CSVBindByPosition\u003c/code\u003e annotation.\u003c/p\u003e\n\u003cp\u003eKeep in mind the OpenCSV positions are 0-based:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-class\"\u003e\u003cspan class=\"hljs-keyword\"\u003eclass\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003eTrees\u003c/span\u003e\u003c/span\u003e{\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByPosition(position = 0, required = true)\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e index;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByPosition(position = 1, required = true)\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e girth;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByPosition(position = 2)\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e height;\n\n \u003cspan class=\"hljs-meta\"\u003e@CSVBindByPosition(position = 3)\u003c/span\u003e\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003eint\u003c/span\u003e volume;\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf you want to handle more complicated scenarios, you can implement a class with the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://opencsv.sourceforge.net/apidocs/com/opencsv/bean/MappingStrategy.html\"\u003eMappingStrategy Interface\u003c/a\u003e and define the translation or mapping schema that suits your parsing scenario.\u003c/p\u003e\n\u003ch3 id=\"writingcsvswithopencsv\"\u003eWriting CSVs with OpenCSV\u003c/h3\u003e\n\u003cp\u003eOpenCSV has more options than Apache Commons CSV when it comes to writing data to CSV files. It allows you to either write from an array of Strings, or write from a list of objects.\u003c/p\u003e\n\u003cp\u003eWriting from a list of objects requires that the objects be initialized and declared beforehand. So to keep things simple, let's consider working with an array of strings.\u003c/p\u003e\n\u003cp\u003eTo generate a CSV file with data from an array of strings:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVWriter csvWriter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVWriter(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileWriter(\u003cspan class=\"hljs-string\"\u003e\u0026quot;new.csv\u0026quot;\u003c/span\u003e), \u003cspan class=\"hljs-string\"\u003e\u0026#x27;,\u0026#x27;\u003c/span\u003e);\nString[] records = \u003cspan class=\"hljs-string\"\u003e\u0026quot;Index.Girth.Height.Volume\u0026quot;\u003c/span\u003e.split(\u003cspan class=\"hljs-string\"\u003e\u0026quot;.\u0026quot;\u003c/span\u003e);\ncsvWriter.writeNext(records);\ncsvWriter.close();\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eOpenCSV works with the concept that CSV is not \u003cem\u003ejust\u003c/em\u003e comma-separated values; it allows you to define which delimiter you want to use in the file as a parameter in the \u003ccode\u003eCSVWriter\u003c/code\u003e constructor.\u003c/p\u003e\n\u003cp\u003eSimilarly, when defining a String array, you may find it useful to declare a String and then separate it into values based on a delimiter. This is especially useful when you need to copy a selected subset of data rows from one CSV or database file to another.\u003c/p\u003e\n\u003cp\u003eWhen initializing the \u003ccode\u003eCSVWriter\u003c/code\u003e, the \u003ccode\u003eFileWriter\u003c/code\u003e or \u003ccode\u003eWriter\u003c/code\u003e is mandatory. Initializing the writer using just one parameter results in a default comma separated file.\u003c/p\u003e\n\u003cp\u003eThere are some additional parameters for specific use cases:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ccode\u003eChar separator\u003c/code\u003e - the delimiter. If undeclared the default delimiter will be a comma.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eChar quotechar\u003c/code\u003e - the quotation character. This will be used in case your dataset contains a value with a comma as part of the dataset, and you need to generate a comma separated file. Generally either double quotes, single quotes or slashes are used as quote characters.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eChar escapechar\u003c/code\u003e - This is generally used to escape the \u003ccode\u003equotechar\u003c/code\u003e.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eString lineend\u003c/code\u003e - the string or character that determines the end of a line of data.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eYou could construct the \u003ccode\u003eCSVWriter\u003c/code\u003e including all optional parameters:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVWriter csvWriter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVWriter(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileWriter(\u003cspan class=\"hljs-string\"\u003e\u0026quot;new.csv\u0026quot;\u003c/span\u003e), \u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;\u0026#x27;\u0026quot;\u003c/span\u003e,\u003cspan class=\"hljs-string\"\u003e\u0026quot;/\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;\\n\u0026quot;\u003c/span\u003e);\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003e\u003ccode\u003eCSVWriter\u003c/code\u003e also has some fields that you can pass as parameters to the constructor. You can define these values as constants and reuse the characters and strings across your codebase to preserve consistency.\u003c/p\u003e\n\u003cp\u003eFor example after declaring:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVWriter.DEFAULT_SEPARATOR = \u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e;\nCSVWriter.DEFAULT_QUOTE_CHARACTER = \u003cspan class=\"hljs-string\"\u003e\u0026quot;\u0026#x27;\u0026quot;\u003c/span\u003e;\nCSVWriter.DEFAULT_ESCAPE_CHARACTER = \u003cspan class=\"hljs-string\"\u003e\u0026quot;/\u0026quot;\u003c/span\u003e;\nCSVWriter.DEFAULT_LINE_END = \u003cspan class=\"hljs-string\"\u003e\u0026quot;\\n\u0026quot;\u003c/span\u003e;\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eYou could use:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVWriter csvWriter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVWriter(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileWriter(\u003cspan class=\"hljs-string\"\u003e\u0026quot;new.csv\u0026quot;\u003c/span\u003e), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eOr make use of OpenCSV using the default values if values are not explicitly defined in the constructor and simply call:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVWriter csvWriter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVWriter(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileWriter(\u003cspan class=\"hljs-string\"\u003e\u0026quot;new.csv\u0026quot;\u003c/span\u003e));\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eSo if your data includes a line with a username and an address, for example: \u003cem\u003eJohnDoe, 19/2, ABC Street, Someplace\u003c/em\u003e, the actual string format that you'd need it to be in is \u003cem\u003e\u0026quot;JohnDoe\u0026quot;, \u0026quot;19//2/, ABC Street/, Someplace\u0026quot;\u003c/em\u003e.\u003c/p\u003e\n\u003ch2 id=\"conclusion\"\u003eConclusion\u003c/h2\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://opencsv.sourceforge.net/\"\u003eOpenCSV\u003c/a\u003e is one of the simplest and easiest CSV parsers to understand, using standard \u003ccode\u003eReader\u003c/code\u003e/\u003ccode\u003eWriter\u003c/code\u003e classes and offering a \u003ccode\u003eCSVReader\u003c/code\u003e implementation on top.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java\"\u003eReading and Writing CSVs in Core Java\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-apache-commons-csv\"\u003eReading and Writing CSVs in Java with Apache Commons CSV\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n","parent_id":null,"type":"article","visibility":"public","img_feature":null,"is_featured":false,"locale":"en","custom_excerpt":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"comment_id":"378","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"read_time_min":7,"published_at":1550671320000,"created_at":1550365108000,"updated_at":1550671320000,"contributors":[{"id":25,"name":"Jean Fernando","slug":"jean","role_id":3,"img_profile":"/assets/images/logo-sa-small.png","img_cover":null,"bio_html":"","website":null,"location":"Sri Lanka","facebook":null,"twitter":null,"github":null,"created_at":1544358993000,"updated_at":1629840515113,"role":"contributor","_pivot_content_id":338,"_pivot_user_id":25,"_pivot_role":"author","_pivot_sort_order":0}],"tags":[{"id":13,"name":"java","slug":"java","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1431358735000,"updated_at":1431358735000,"_pivot_content_id":338,"_pivot_tag_id":13,"_pivot_sort_order":0}],"_pivot_user_id":25,"_pivot_content_id":338},{"id":337,"title":"Reading and Writing CSVs in Java with Apache Commons CSV","slug":"reading-and-writing-csvs-in-java-with-apache-commons-csv","body_html":"\u003ch2 id=\"introduction\"\u003eIntroduction\u003c/h2\u003e\n\u003cp\u003eThis is the second article in a short series dedicated to \u003ca target=\"_blank\" href=\"/libraries-for-reading-and-writing-csvs-in-java\"\u003eLibraries for Reading and Writing CSVs in Java\u003c/a\u003e, and a direct continuation from the previous article - \u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java\"\u003eReading and Writing CSVs in Core Java\u003c/a\u003e.\u003c/p\u003e\n\u003ch2 id=\"apachecommonscsv\"\u003eApache Commons CSV\u003c/h2\u003e\n\u003cp\u003eThe \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://commons.apache.org/proper/commons-csv/download_csv.cgi\"\u003eApache Commons CSV\u003c/a\u003e library is the Apache Software Foundation's version of a Java CSV parser. According to the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://commons.apache.org/proper/commons-csv/summary.html\"\u003eproject summary\u003c/a\u003e, it attempts to \u0026quot;\u003cem\u003eprovide a simple interface for reading and writing CSV files of various types\u003c/em\u003e\u0026quot;.\u003c/p\u003e\n\u003cp\u003eAs with all libraries associated with Apache, it operates with an \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.apache.org/licenses/LICENSE-2.0\"\u003eApache license\u003c/a\u003e, meaning it can be used, distributed and modified freely.\u003c/p\u003e\n\u003cp\u003eApache Commons allows developers to define their own formats, but offers a predefined set of formats with its \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html\"\u003eCSVFormat class\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eThese predefined formats are:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003cstrong\u003eRFC4180\u003c/strong\u003e - comma separated format defined by RFC 4180.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eDEFAULT\u003c/strong\u003e - Similar to RFC4180 format, but allows empty lines in between rows of data. This format is used if not otherwise specified when you define a parser with the Apache Commons CSV library.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eEXCEL\u003c/strong\u003e - Similar to RFC 4180, but allows missing column names, and ignores empty lines.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eTDF\u003c/strong\u003e - Predefined format for CSV files which are delimited with tabs (\u003ccode\u003e\\t\u003c/code\u003e) instead of commas.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eMYSQL\u003c/strong\u003e - The format supported by MySql's \u003ccode\u003eSELECT INTO OUTFILE\u003c/code\u003e and \u003ccode\u003eLOAD DATA INFILE\u003c/code\u003e operations.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eORACLE\u003c/strong\u003e - CSV format used by the Oracle SQL loader.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eINFORMIX_UNLOAD and INFORMIX_UNLOAD_CSV\u003c/strong\u003e - specialized formats defined for use with the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.ibm.com/analytics/informix\"\u003eIBM Informix\u003c/a\u003e embeddable database.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003eMONGODB_CSV and MONGODB_TSV\u003c/strong\u003e - Works with the poplar NoSQL \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.mongodb.com/\"\u003eMongoDB\u003c/a\u003e database, for comma separated and tab separated values respectively.\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003ePOSTGRESQL_CSV and POSTGRESQL_TEXT\u003c/strong\u003e - Format supported by PostgreSQL databases.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eIf you need a very simple solution, Apache Commons CSV might not be suitable. The implementation is designed to offer maximum flexibility, which makes the source code equate to around 30,000 lines, and prove quite complex to understand in some cases.\u003c/p\u003e\n\u003cp\u003eHowever, if you do need to cover a wide variety of formats, Apache Commons is a trusted library, well maintained and updated regularly, with extensive Java Docs and developer documentation to support beginners.\u003c/p\u003e\n\u003cp\u003eIt is included in the Maven Central repository and has no external dependencies.\u003c/p\u003e\n\u003ch3 id=\"readingcsvswithapachecommonscsv\"\u003eReading CSVs with Apache Commons CSV\u003c/h3\u003e\n\u003cp\u003eThe Apache Commons library offers several methods to access individual fields in a CSV file. If you're working with apps like Excel, then your Excel formatted CSV is likely to have a header.\u003c/p\u003e\n\u003cp\u003eHowever, if you're using CSV as a basic comma-separated set of text, to transfer data between systems or to feed into another processing application, the file may contain data starting from the first row itself, without a header. The Apache Commons CSV library takes both these scenarios into consideration.\u003c/p\u003e\n\u003cp\u003eIf your CSV file does not contain a header, or if you're unsure whether it does, you can use the index to access a record. Since \u003ccode\u003eCSVRecord\u003c/code\u003e implements the \u003cem\u003eJava Iterable Interface\u003c/em\u003e, the index is 0-based, even though CSV indexes, when opened with Excel and most other applications, start with 1:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVParser csvParser = CSVFormat.DEFAULT.parse(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e InputStreamReader(csvFile.getInputStream()));\n\u003cspan class=\"hljs-keyword\"\u003efor\u003c/span\u003e (CSVRecord record : csvParser) {\n String field_1 = record.get(\u003cspan class=\"hljs-number\"\u003e0\u003c/span\u003e);\n String field_2 = record.get(\u003cspan class=\"hljs-number\"\u003e1\u003c/span\u003e);\n ...\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf you're sure that the CSV file you need to parse has a header, and you know the header format before processing, you can use the header column string to fetch records.\u003c/p\u003e\n\u003cp\u003eLet's consider \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://people.sc.fsu.edu/~jburkardt/data/csv/trees.csv\"\u003ea sample CSV file\u003c/a\u003e with tree data, and call it the 'tree data CSV' for future reference in this article:\u003c/p\u003e\n\u003ctable class=\"table\"\u003e\n \u003ctr\u003e\n \u003cth\u003e\u003cspan style=\"font-weight:bold\"\u003eIndex\u003c/span\u003e\u003c/th\u003e\n \u003cth\u003eGirth (in)\u003c/th\u003e\n \u003cth\u003eHeight (ft)\u003c/th\u003e\n \u003cth\u003eVolume (ft)\u003c/th\u003e\n \u003c/tr\u003e\n \u003ctr\u003e\n \u003ctd\u003e1\u003c/td\u003e\n \u003ctd\u003e8.3\u003c/td\u003e\n \u003ctd\u003e70\u003c/td\u003e\n \u003ctd\u003e10.3\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/table\u003e\n\u003cp\u003eTo refer to each row of data we could use indexing as with the previous example, or the column header:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eInputStreamReader input = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e InputStreamReader(csvFile.getInputStream());\nCSVParser csvParser = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(input);\n\u003cspan class=\"hljs-keyword\"\u003efor\u003c/span\u003e (CSVRecord record : csvParser) {\n String field_1 = record.get(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Index\u0026quot;\u003c/span\u003e);\n String field_2 = record.get(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Girth (in)\u0026quot;\u003c/span\u003e);\n String field_3 = record.get(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Height (ft)\u0026quot;\u003c/span\u003e);\n String field_4 = record.get(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Volume (ft)\u0026quot;\u003c/span\u003e);\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf you want to read a file that does not contain a header row, want to define your own header, or find indexing confusing, Apache Commons also allows the definition of a header for parsing.\u003c/p\u003e\n\u003cp\u003eInstead of using the \u003ccode\u003e.withFirstRecordAsHeader()\u003c/code\u003e method when defining the format of the CSV file, you can define a header manually. For example, if you want to avoid referring to the units of measure in the header in our tree data file, you can redefine the header to use your own string values:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVParser csvParser = CSVFormat.REF4180.withHeader(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Index\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Girth\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Height\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Volume\u0026quot;\u003c/span\u003e);\n\u003cspan class=\"hljs-keyword\"\u003efor\u003c/span\u003e (CSVRecord record : csvParser) {\n String field_2 = record.get(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Girth\u0026quot;\u003c/span\u003e);\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf your CSV file contains a header, but you want to define your own header and skip reading the header in the file, use \u003ccode\u003e.readNext()\u003c/code\u003e to skip the first row:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVRecord header = csvParser.readNext();\n\u003cspan class=\"hljs-comment\"\u003e// read the other rows in a loop as usual\u003c/span\u003e\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eYou can also use an enumeration to define a header if you need to use your definition across multiple classes as in this example:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-class\"\u003e\u003cspan class=\"hljs-keyword\"\u003eenum\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003etreeHeader\u003c/span\u003e \u003c/span\u003e{\n Index, Girth, Height, Volume\n}\n...\nCSVParser csvParser = CSVFormat.DEFAULT.withHeader(treeHeader.class).parse(input);\n\u003cspan class=\"hljs-comment\"\u003e// read rows\u003c/span\u003e\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eThe Apache Commons CSV library has some \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVRecord.html\"\u003eadditional methods\u003c/a\u003e to make parsing easier, including:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ccode\u003e.getRecordNumber()\u003c/code\u003e - returns the number assigned to the record in the CSV file.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e.isConsistent()\u003c/code\u003e - can be used for error handling, returns True or False based on whether the size of the current record matches the size of the header row.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e.size()\u003c/code\u003e - can be used to determine the number of values in the record.\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003e.toString()\u003c/code\u003e - returns the record as a String. Useful when you need to store the entire row as a String for later processing, hashing or comparison.\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch3 id=\"writingcsvswithapachecommonscsv\"\u003eWriting CSVs with Apache Commons CSV\u003c/h3\u003e\n\u003cp\u003eThe Apache Commons CSV library is mostly focused on reading data from CSV files. When it comes to writing, the methods recommended in the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://commons.apache.org/proper/commons-csv/user-guide.html\"\u003euser guide\u003c/a\u003e are quite familiar to our core Java implementation.\u003c/p\u003e\n\u003cp\u003eHowever, there are some useful additional features, such as the ability to define a header when printing, and printing directly from a JDBC \u003ccode\u003eResultSet\u003c/code\u003e object.\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003eCSVPrinter\u003c/code\u003e class implements the \u003ccode\u003eFlushable\u003c/code\u003e and \u003ccode\u003eCloseable\u003c/code\u003e interfaces, making its behavior similar to an extension of a usual Java \u003ccode\u003eWriter\u003c/code\u003e or \u003ccode\u003eStringWriter\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eFor example to generate our tree data CSV:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVPrinter csvPrinter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e CSVPrinter(stringWriter, CSVFormat.DEFAULT.withHeader(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Index\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Girth\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Height\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Volume\u0026quot;\u003c/span\u003e));\ncsvPrinter.printRecord(\u003cspan class=\"hljs-string\"\u003e\u0026quot;1\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;8.3\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;70\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;10.3\u0026quot;\u003c/span\u003e);\ncsvPrinter.flush();\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eThe method \u003ccode\u003e.withHeader()\u003c/code\u003e accepts the String values of the header as parameters.\u003c/p\u003e\n\u003cp\u003eIf you want to generate a CSV without a header you can simply use \u003ccode\u003enew CSVPrinter(stringWriter, CSVFormat.DEFAULT);\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eIf you want to use the column headers from an existing JDBC \u003ccode\u003eResultSet\u003c/code\u003e for printing, Apache Commons CSV accepts a \u003ccode\u003eResultSet\u003c/code\u003e as a parameter for its \u003ccode\u003e.withHeader()\u003c/code\u003e method:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eCSVPrinter csvPrinter = CSVFormat.RFC4180.withHeader(treeDataResultSet).print(out);\ncsvPrinter.printRecords(treeDataResultSet);\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eIf the \u003ccode\u003eCSVFormat\u003c/code\u003e you use allows empty lines, you can use \u003ccode\u003ecsvPrinter.println()\u003c/code\u003e to print a blank line in between data rows.\u003c/p\u003e\n\u003cp\u003eIn addition to a \u003ccode\u003eResultSet\u003c/code\u003e, the \u003ccode\u003e.printRecords()\u003c/code\u003e method is also able to work with an \u003cem\u003eIterable Array\u003c/em\u003e, or a \u003cem\u003eCollection\u003c/em\u003e of Strings passed to the method using \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html\"\u003evarArgs\u003c/a\u003e.\u003c/p\u003e\n\u003ch2 id=\"conclusion\"\u003eConclusion\u003c/h2\u003e\n\u003cp\u003eApache Commons CSV attempts to provide a simple interface for reading and writing CSV files of various types.\u003c/p\u003e\n\u003cp\u003eThe implementation is designed to offer maximum flexibility, which makes the source code quite complex to understand in some cases.\u003c/p\u003e\n\u003cp\u003eHowever, if you do need to cover a wide variety of formats, Apache Commons is a trusted library, well maintained and updated regularly, with extensive Java Docs and developer documentation to support beginners.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java\"\u003eReading and Writing CSVs in Core Java\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-opencsv/\"\u003eReading and Writing CSVs in Java with OpenCSV\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n","parent_id":null,"type":"article","visibility":"public","img_feature":null,"is_featured":false,"locale":"en","custom_excerpt":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"comment_id":"377","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"read_time_min":7,"published_at":1550585940000,"created_at":1550364337000,"updated_at":1550679805000,"contributors":[{"id":25,"name":"Jean Fernando","slug":"jean","role_id":3,"img_profile":"/assets/images/logo-sa-small.png","img_cover":null,"bio_html":"","website":null,"location":"Sri Lanka","facebook":null,"twitter":null,"github":null,"created_at":1544358993000,"updated_at":1629840515113,"role":"contributor","_pivot_content_id":337,"_pivot_user_id":25,"_pivot_role":"author","_pivot_sort_order":0}],"tags":[{"id":13,"name":"java","slug":"java","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1431358735000,"updated_at":1431358735000,"_pivot_content_id":337,"_pivot_tag_id":13,"_pivot_sort_order":0}],"_pivot_user_id":25,"_pivot_content_id":337},{"id":289,"title":"Libraries for Reading and Writing CSVs in Java","slug":"libraries-for-reading-and-writing-csvs-in-java","body_html":"\u003ch2 id=\"introduction\"\u003eIntroduction\u003c/h2\u003e\n\u003cp\u003e\u003cstrong\u003eCSV\u003c/strong\u003e stands for \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://en.wikipedia.org/wiki/Comma-separated_values\"\u003eComma Separated Values\u003c/a\u003e, a method of formatting data which has been used even before the use of personal computers became widespread. The format gained popularity because the first computers used \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://en.wikipedia.org/wiki/Punched_card\"\u003epunched cards\u003c/a\u003e to process data, and comma separated values are easier to 'punch' in than traditional table-formatted data in fixed columns.\u003c/p\u003e\n\u003cp\u003eIn the present, CSV files are typically used to transfer data between applications or systems, acting as a common format to export data from one system and import it back to another.\u003c/p\u003e\n\u003cp\u003eThe CSV format typically consists of information organized with lines consisting of multiple fields separated by a delimiter, with one line equivalent to one data record. The delimiter can be a comma, semi-colon, or tab character. There is no specific format, although a specification called \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://tools.ietf.org/html/rfc4180\"\u003eRFC 4180\u003c/a\u003e has been introduced in an attempt to standardize how a CSV file should ideally behave.\u003c/p\u003e\n\u003ch2 id=\"rfc4180\"\u003eRFC 4180\u003c/h2\u003e\n\u003cp\u003e\u0026quot;RFC\u0026quot; stands for \u003cstrong\u003eRequest for Comments\u003c/strong\u003e, meaning that the document is just meant to be a set of common specifications or guidelines, and not accepted rules.\u003c/p\u003e\n\u003cp\u003eThere are many deviations from the specified format in the way CSV files are generated and read by modern applications, but most systems adhere to the initial guidelines set out by RFC 4180.\u003c/p\u003e\n\u003cp\u003eAccording to RFC 4180, CSV files should have the following commonalities:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eEach record should be on a separate line, with a character break at the end of the line.\u003c/li\u003e\n\u003cli\u003eThere may or may not be a header line. The presence of a header can be specified in the \u003ccode\u003eheader\u003c/code\u003e parameter of the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types\"\u003eMIME type\u003c/a\u003e.\u003c/li\u003e\n\u003cli\u003eThe MIME type for CSV files officially registered with \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.iana.org/\"\u003eIANA\u003c/a\u003e is \u0026quot;text/csv\u0026quot;.\u003c/li\u003e\n\u003cli\u003eEach record may consist of one or more comma-separated fields, and the same number of fields should persist throughout the file (there should be an equal number of fields in all records).\u003c/li\u003e\n\u003cli\u003eIf a field contains commas, line breaks or double-quotes, it should be enclosed in double quotes itself.\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch2 id=\"whyusecsvsforiooperationswithjava\"\u003eWhy use CSVs for IO operations with Java\u003c/h2\u003e\n\u003cp\u003eCSV files are human-readable, and simpler to understand than other data transfer formats like \u003ca target=\"_blank\" href=\"/reading-and-writing-xml-in-java\"\u003eXML\u003c/a\u003e or \u003ca target=\"_blank\" href=\"/reading-and-writing-json-in-java\"\u003eJSON\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eCSV files can be imported into most spreadsheet applications like \u003cem\u003eExcel\u003c/em\u003e, \u003cem\u003eGoogle Sheets\u003c/em\u003e and \u003cem\u003eOpenOffice Calc\u003c/em\u003e, and easy to generate - an existing \u003ccode\u003e.xls\u003c/code\u003e or \u003ccode\u003e.xlsx\u003c/code\u003e file can be converted to CSV format within Excel itself, as long as the file contains just characters and not any macros, images, etc.\u003c/p\u003e\n\u003cp\u003eThe format is compact and usually smaller in size, leading to faster processing and generation. In comparison, XML requires repetition; typically the column header name is repeated twice for each row with the start and end tags (along with syntax-related characters), whereas CSV only requires the column headers once, usually in the very first row.\u003c/p\u003e\n\u003cp\u003eGiven all these factors, being able to read from and write to CSV files is a key skill for any Java developer.\u003c/p\u003e\n\u003ch2 id=\"readingandwritingcsvsincorejava\"\u003eReading and Writing CSVs in Core Java\u003c/h2\u003e\n\u003cp\u003eOwning to the popularity and widespread use of CSV as a format for data transfer, there are many parser libraries that can be used along with Java.\u003c/p\u003e\n\u003cp\u003eThird-party parsers define common formats and are able to work with various delimiters, handle special characters, and sometimes even read non-binary data. However, it is still important to be able to handle CSV files with core Java, without the use of any additional libraries.\u003c/p\u003e\n\u003cp\u003eA simple combination of \u003ccode\u003eFileReader\u003c/code\u003e, \u003ccode\u003eBufferedReader\u003c/code\u003e, and \u003ccode\u003eString.split()\u003c/code\u003e can facilitate reading data from CSVs.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java\"\u003eReading and Writing CSVs Java\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch2 id=\"readingandwritingcsvswithapachecommonscsv\"\u003eReading and Writing CSVs with Apache Commons CSV\u003c/h2\u003e\n\u003cp\u003eThe \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://commons.apache.org/proper/commons-csv/download_csv.cgi\"\u003eApache Commons CSV\u003c/a\u003e library is the Apache Software Foundation's version of a Java CSV parser. According to the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://commons.apache.org/proper/commons-csv/summary.html\"\u003eproject summary\u003c/a\u003e, it attempts to \u0026quot;\u003cem\u003eprovide a simple interface for reading and writing CSV files of various types\u003c/em\u003e\u0026quot;.\u003c/p\u003e\n\u003cp\u003eAs with all libraries associated with Apache, it operates with an \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.apache.org/licenses/LICENSE-2.0\"\u003eApache license\u003c/a\u003e, meaning it can be used, distributed, and modified freely.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-apache-commons-csv/\"\u003eReading and Writing CSVs in Java with Apache Commons CSV\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch2 id=\"readingandwritingcsvswithopencsv\"\u003eReading and Writing CSVs with OpenCSV\u003c/h2\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://opencsv.sourceforge.net/\"\u003eOpenCSV\u003c/a\u003e is one of the simplest and easiest CSV parsers to understand, using standard \u003ccode\u003eReader\u003c/code\u003e/\u003ccode\u003eWriter\u003c/code\u003e classes and offering a \u003ccode\u003eCSVReader\u003c/code\u003e implementation on top.\u003c/p\u003e\n\u003cp\u003eJust like Apache Commons CSV, \u003cem\u003eOpenCSV\u003c/em\u003e operates with an Apache 2.0 license. Before downloading and deciding whether to use OpenCSVs parsers, you can browse through the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://sourceforge.net/p/opencsv/source/ci/master/tree/\"\u003esource code and Java docs\u003c/a\u003e, and even check out their JUnit test suite, which is included in their git repository.\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-opencsv/\"\u003eReading and Writing CSVs in Java with OpenCSV\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003ch2 id=\"thirdpartylibrariesforcsviooperations\"\u003eThird-Party Libraries for CSV IO operations\u003c/h2\u003e\n\u003cp\u003eKnowing how to read from and write to a CSV file in core Java is important, and usually sufficient for most basic operations. However, there are instances where relying on a third party library is the way to go.\u003c/p\u003e\n\u003cp\u003eFor example, our own usage of \u003ccode\u003esplit\u003c/code\u003e to parse a CSV file, without using libraries above, would fail if the fields themselves contained commas. We could extend our logic to fit this scenario, but why reinvent the wheel when there are already proven solutions available for use?\u003c/p\u003e\n\u003cp\u003eThis is where libraries come in - most of them support various configurations and identify escape characters and end of file characters frequently used by different systems, product suites and databases so that we don't have to track, implement, and unit-test each configuration ourselves.\u003c/p\u003e\n\u003cp\u003eThere are a multitude of different parsing libraries available for free, each with different strengths. Let's consider some of the more popular libraries for CSV parsing with Java, comparing their pros and cons.\u003c/p\u003e\n\u003ch2 id=\"othercsvlibraries\"\u003eOther CSV libraries\u003c/h2\u003e\n\u003cp\u003eIn addition to Apache Commons CSV and OpenCSV, there are a variety of other CSV parsers available for use. Let's take a quick look at some of the other libraries, and \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://github.com/robert-bor/CSVeed/wiki/Comparison-of-Java-CSV-libraries\"\u003ecompare their usage\u003c/a\u003e, advantages, and disadvantages:\u003c/p\u003e\n\u003ch3 id=\"supercsv\"\u003eSuperCSV\u003c/h3\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://super-csv.github.io/super-csv/index.html\"\u003eSuperCSV\u003c/a\u003e is another dominant CSV parsing library. The SuperCSV implementation supports formats that are not considered by other mainstream parsers.\u003c/p\u003e\n\u003cp\u003eSimilar to the OpenCSV annotation methods, SuperCSV offers POJO support for dealing with Java Beans, in addition to the usual lists and maps.\u003c/p\u003e\n\u003cp\u003eEncoding and decoding is also handled by the library as long as the file is compliant to the format outlined in the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://super-csv.github.io/super-csv/csv_specification.html\"\u003eSuperCSV specification\u003c/a\u003e. If the file is non-compliant, you can still define a custom delimiter, quote character, or new line character as required, or extend the source code to facilitate specific requirements.\u003c/p\u003e\n\u003cp\u003eParsing is made easier by data formatting options available with SuperCSV, which allows trimming and regex replacements while processing. The library also supports stream-based input and output, making it manageable in terms of performance and memory constrained systems.\u003c/p\u003e\n\u003cp\u003eThe SuperCSV library also allows partial reading and partial writing, which is not supported by the other CSV parsers we've discussed throughout this article. You can choose to set specific header column values to \u003ccode\u003enull\u003c/code\u003e and proceed with processing the remaining columns, or write a dataset which contains optional values without adding your own error-handling.\u003c/p\u003e\n\u003cp\u003eA big downside that deserves mentioning is that the library does not appear to be maintained - the last published date is four years ago - in 2015, although the \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://github.com/super-csv/super-csv\"\u003egit repository\u003c/a\u003e has more recent contributions.\u003c/p\u003e\n\u003ch3 id=\"univocitycsvparser\"\u003eUniVocity CSV Parser\u003c/h3\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.univocity.com/pages/about-parsers\"\u003eUniVocity CSV Parser\u003c/a\u003e loudly \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://github.com/uniVocity/csv-parsers-comparison\"\u003eclaims to be the fastest CSV parser\u003c/a\u003e in a 2018 comparison among 18 different publicly available CSV parsers. UniVocity parser allows you to select the fields you want to parse, skipping the unnecessary or non-mandatory fields in a single file, giving it the ability to filter columns of a CSV.\u003c/p\u003e\n\u003cp\u003eIt has more customization options than OpenCSV and Apache Commons CSV, which makes it harder to set up and get started with. Code readability might also be less when compared to some other libraries since UniVocity parser requires that the format, line separator, and header extraction method be declared before parsing is attempted.\u003c/p\u003e\n\u003cp\u003eOn the positive side, the variety of \u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://www.univocity.com/pages/univocity_parsers_tutorial#settings\"\u003eformatting and customization options\u003c/a\u003e makes it suitable to deal with the 'edge-cases' involving CSV files which are not RFC4180 compliant.\u003c/p\u003e\n\u003cp\u003eSimilar to both OpenCSV and Apache Commons CSV, you can use either an iterator or defined parser class (in this case either \u003ccode\u003eCsvParser\u003c/code\u003e or \u003ccode\u003eTsvParser\u003c/code\u003e). UniVocity CSV Parsers also supports reading into beans with a more complex setup when compared to OpenCSV.\u003c/p\u003e\n\u003cp\u003eWriting is a similarly complicated, but configurable process with the UniVocity CSV parser, with specific use cases like the ability to define value conversions and column selection. Writing directly from a map or annotated Java Beans is also supported.\u003c/p\u003e\n\u003ch3 id=\"flatpackcsvparser\"\u003eFlatPack CSV Parser\u003c/h3\u003e\n\u003cp\u003e\u003ca rel=\"nofollow\" target=\"_blank\" href=\"http://flatpack.sourceforge.net/\"\u003eFlatPack CSV Parser\u003c/a\u003e is fast and more suitable for handling extremely large files, dealing with file sorting before parsing and fixed width parsing. It can be used in a scenario where your CSV does not have a specific delimiter but is comprised of fixed width text for example. The parser also supports column mapping through XML specifications, where the fields in the XML and the data fields in the CSV are in the same order.\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003eBuffReaderDelimiterFactory\u003c/code\u003e allows streaming larger files to avoid everything being contained in memory when parsing data. Columns can also be added, removed, or ignored as needed.\u003c/p\u003e\n\u003cp\u003eSince the library focusses on being friendly for larger files, it also allows the option to exclude bad data, and add it to an error collection for later processing. This avoids a massive dataset needing to be re-processed due to one or two errors and simplifies error handling.\u003c/p\u003e\n\u003cp\u003eThe library \u003cem\u003eis\u003c/em\u003e currently maintained, with the most recent publication being in 2019. It has specific strengths, but can be complicated to set up and understand due to the multitude of options and customization features introduced to handle very specific scenarios that are not RFC4180 compliant.\u003c/p\u003e\n\u003ch2 id=\"conclusion\"\u003eConclusion\u003c/h2\u003e\n\u003cp\u003eThe most basic CSV reading and writing scenarios can be handled using core Java IO with \u003ccode\u003eBufferedReader\u003c/code\u003es , \u003ccode\u003eFileWriter\u003c/code\u003es, and customized error handling. However, external libraries provide tried and tested solutions when it comes to more complex operations that involve being able to support larger files that may or may not be RFC 4180 compliant, with varying delimiters and different requirements.\u003c/p\u003e\n\u003cp\u003eThe performance and flexibility of your application depend on the option you choose - some parsers are better at memory management, while others are more flexible and customizable.\u003c/p\u003e\n\u003cp\u003eYou can use this article as a guide to identify which library best suits your needs, and learn the basics of CSV file handling, and reading and writing CSVs in Java.\u003c/p\u003e\n","parent_id":null,"type":"article","visibility":"public","img_feature":null,"is_featured":false,"locale":"en","custom_excerpt":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"comment_id":"329","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"read_time_min":9,"published_at":1550500500000,"created_at":1544673911000,"updated_at":1550679846000,"contributors":[{"id":25,"name":"Jean Fernando","slug":"jean","role_id":3,"img_profile":"/assets/images/logo-sa-small.png","img_cover":null,"bio_html":"","website":null,"location":"Sri Lanka","facebook":null,"twitter":null,"github":null,"created_at":1544358993000,"updated_at":1629840515113,"role":"contributor","_pivot_content_id":289,"_pivot_user_id":25,"_pivot_role":"author","_pivot_sort_order":0}],"tags":[{"id":13,"name":"java","slug":"java","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1431358735000,"updated_at":1431358735000,"_pivot_content_id":289,"_pivot_tag_id":13,"_pivot_sort_order":0}],"_pivot_user_id":25,"_pivot_content_id":289},{"id":336,"title":"Reading and Writing CSVs in Java","slug":"reading-and-writing-csvs-in-java","body_html":"\u003ch2 id=\"introduction\"\u003eIntroduction\u003c/h2\u003e\n\u003cp\u003eThis is the first article in a short series dedicated to \u003ca target=\"_blank\" href=\"/libraries-for-reading-and-writing-csvs-in-java\"\u003eLibraries for Reading and Writing CSVs in Java\u003c/a\u003e.\u003c/p\u003e\n\u003ch2 id=\"readingandwritingcsvsincorejava\"\u003eReading and Writing CSVs in Core Java\u003c/h2\u003e\n\u003cp\u003eOwning to the popularity and widespread use of CSV as a format for data transfer, there are many parser libraries that can be used along with Java.\u003c/p\u003e\n\u003cp\u003eThird-party parsers define common formats and are able to work with various delimiters, handle special characters, and sometimes even read non-binary data. However, not all programs require all of those features, so it is still important to be able to handle CSV files with core Java, without the use of any additional libraries.\u003c/p\u003e\n\u003cp\u003eA simple combination of \u003ccode\u003eFileReader\u003c/code\u003e, \u003ccode\u003eBufferedReader\u003c/code\u003e, and \u003ccode\u003eString.split()\u003c/code\u003e can facilitate reading data from CSVs. Let's consider the steps to open a basic CSV file and parse the data it contains:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eUse \u003ccode\u003eFileReader\u003c/code\u003e to open the CSV file\u003c/li\u003e\n\u003cli\u003eCreate a \u003ccode\u003eBufferedReader\u003c/code\u003e and \u003ca target=\"_blank\" href=\"https://stackabuse.com/reading-a-file-line-by-line-in-java/\"\u003eread the file line by line\u003c/a\u003e until an \u0026quot;End of File\u0026quot; (\u003ca rel=\"nofollow\" target=\"_blank\" href=\"https://en.wikipedia.org/wiki/End-of-file\"\u003eEOF\u003c/a\u003e) character is reached\u003c/li\u003e\n\u003cli\u003eUse the \u003ccode\u003eString.split()\u003c/code\u003e method to identify the comma delimiter and split the row into fields\u003c/li\u003e\n\u003c/ul\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eBufferedReader csvReader = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e BufferedReader(\u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileReader(pathToCsv));\n\u003cspan class=\"hljs-keyword\"\u003ewhile\u003c/span\u003e ((row = csvReader.readLine()) != \u003cspan class=\"hljs-keyword\"\u003enull\u003c/span\u003e) {\n String[] data = row.split(\u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e);\n \u003cspan class=\"hljs-comment\"\u003e// do something with the data\u003c/span\u003e\n}\ncsvReader.close();\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eThe \u003ccode\u003edata\u003c/code\u003e String array will contain a list of the fields in each row of the file found in the \u003ccode\u003epathToCsv\u003c/code\u003e file location. If the CSV file has a delimiter other than a comma, it can be specified in the \u003ccode\u003esplit\u003c/code\u003e method. For example, another common delimeter is the tab for Tab-Separated-Value (TSV) files.\u003c/p\u003e\n\u003cp\u003eThe data can be passed to a separate method for processing or writing to a database from within the loop, or stored in a \u003ca target=\"_blank\" href=\"/java-collections-the-list-interface/\"\u003eJava Collection\u003c/a\u003e for later use. For example, if you're writing a large amount of data to a database, constraint violations (like primary key violations, for example) caused by human errors when generating the CSV, can be avoided by using a hash map. If there is a duplicate entry in the CSV, the hash map will store the most recent 'read' and overwrite the previous entry.\u003c/p\u003e\n\u003cp\u003eSince you're writing the parser from scratch you'll need to take care of simple error handling on your own. Like if you're unsure whether the file exists, it is always safer to enclose the read operation within a \u003ccode\u003etry/catch\u003c/code\u003e block or add logic to determine the existence of the file before processing.\u003c/p\u003e\n\u003cp\u003eTo check whether the file exists, the following modification can be made to our code:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003eFile csvFile = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e File(pathToCsv);\n\u003cspan class=\"hljs-keyword\"\u003eif\u003c/span\u003e (csvFile.isFile()) {\n \u003cspan class=\"hljs-comment\"\u003e// create BufferedReader and read data from csv\u003c/span\u003e\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eWith some simple error handling and strict requirements on the CSV format, creating a simple parser yourself using core Java components is a task most programmers can take on.\u003c/p\u003e\n\u003ch3 id=\"writingcsvsincorejava\"\u003eWriting CSVs in Core Java\u003c/h3\u003e\n\u003cp\u003eMost third-party parsers also support writing to CSV files. However, there is a simple way to write to CSV files, just like any other file type, without using any libraries.\u003c/p\u003e\n\u003cp\u003eThe simplest way is to use a \u003ccode\u003eFileWriter\u003c/code\u003e object and treat the CSV file as any other text file. In our example the data is stored in some \u003ccode\u003eList\u003c/code\u003e objects, which we just iterate over and append to the writer:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-comment\"\u003e// Our example data\u003c/span\u003e\nList\u0026lt;List\u0026lt;String\u0026gt;\u0026gt; rows = Arrays.asList(\n Arrays.asList(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Jean\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;author\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Java\u0026quot;\u003c/span\u003e),\n Arrays.asList(\u003cspan class=\"hljs-string\"\u003e\u0026quot;David\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;editor\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Python\u0026quot;\u003c/span\u003e),\n Arrays.asList(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Scott\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;editor\u0026quot;\u003c/span\u003e, \u003cspan class=\"hljs-string\"\u003e\u0026quot;Node.js\u0026quot;\u003c/span\u003e)\n);\n\nFileWriter csvWriter = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e FileWriter(\u003cspan class=\"hljs-string\"\u003e\u0026quot;new.csv\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Name\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Role\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;Topic\u0026quot;\u003c/span\u003e);\ncsvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;\\n\u0026quot;\u003c/span\u003e);\n\n\u003cspan class=\"hljs-keyword\"\u003efor\u003c/span\u003e (List\u0026lt;String\u0026gt; rowData : rows) {\n csvWriter.append(String.join(\u003cspan class=\"hljs-string\"\u003e\u0026quot;,\u0026quot;\u003c/span\u003e, rowData));\n csvWriter.append(\u003cspan class=\"hljs-string\"\u003e\u0026quot;\\n\u0026quot;\u003c/span\u003e);\n}\n\ncsvWriter.flush();\ncsvWriter.close();\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eWhen using a \u003ccode\u003eFileWriter\u003c/code\u003e always make sure you flush and close the stream. This improves the performance of the IO operation and indicates that there is no more data to be written to the output stream.\u003c/p\u003e\n\u003ch2 id=\"conclusion\"\u003eConclusion\u003c/h2\u003e\n\u003cp\u003eThere are several ways to read and write CSV files in Java, the simplest being using Core Java components. Although this also leaves you more vulnerable to bugs and will be less robust than a proven third-party solution.\u003c/p\u003e\n\u003cp\u003eIf you're interested in reading about other ways to read and write CSVs in Java, make sure to check out our other articles:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-apache-commons-csv/\"\u003eReading and Writing CSVs in Java with Apache Commons CSV\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca target=\"_blank\" href=\"/reading-and-writing-csvs-in-java-with-opencsv/\"\u003eReading and Writing CSVs in Java with OpenCSV\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n","parent_id":null,"type":"article","visibility":"public","img_feature":null,"is_featured":false,"locale":"en","custom_excerpt":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"comment_id":"376","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"read_time_min":4,"published_at":1550499540000,"created_at":1550363528000,"updated_at":1550679871000,"contributors":[{"id":25,"name":"Jean Fernando","slug":"jean","role_id":3,"img_profile":"/assets/images/logo-sa-small.png","img_cover":null,"bio_html":"","website":null,"location":"Sri Lanka","facebook":null,"twitter":null,"github":null,"created_at":1544358993000,"updated_at":1629840515113,"role":"contributor","_pivot_content_id":336,"_pivot_user_id":25,"_pivot_role":"author","_pivot_sort_order":0}],"tags":[{"id":13,"name":"java","slug":"java","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1431358735000,"updated_at":1431358735000,"_pivot_content_id":336,"_pivot_tag_id":13,"_pivot_sort_order":0}],"_pivot_user_id":25,"_pivot_content_id":336}],"content_count":"4"},"page":1,"error":null},"__N_SSP":true},"page":"/author/[...slug]","query":{"slug":["jean"]},"buildId":"1740324887682","isFallback":false,"isExperimentalCompile":false,"gssp":true,"scriptLoader":[]}</script></body></html>