
OpenCSV is a CSV file parsing library that can make reading from CSV files a lot easier. Reading CSV Files by Using the OpenCSV Library Just like the BufferedReader, the Scanner class approach cannot be used for complex CSV files. List lineData = Arrays.asList(s.nextLine().split(",")) //splitting lines String filePath = "C:\\Users\\Lenovo\\Desktop\\demo.csv" //file path Then we can store individual records in a list of lists. We will simply read each line of the file and then split it by using the comma as a delimiter. This approach is quite similar to that of BufferedReader. We can also use the Scanner class of java.util package to read a CSV file. We also don't require the quotation marks.Įxclamation "!" Reading CSV Files by Using the Scanner Class We can see that no value is stored for the second row of the file(the comma has been omitted). If we try to read this file, then the following data is stored in the lists. For example, consider a CSV file in which the second column is used to store punctuation marks. Note that we cannot use this approach to read more complex CSV files or files in which the comma itself is a value. List lineData = Arrays.asList(line.split(",")) //splitting lines String file = "C:\\Users\\Lenovo\\Desktop\\demo.csv" //file pathīufferedReader br = new BufferedReader(fr)

List > data = new ArrayList() //list of lists to store data Then we can split the line using the split() method and passing the comma as a delimiter. We will simply read each line of the file by using the readLine() method.

The BufferedReader class of the java.io package can be used to read a basic CSV file. Justin, 101, 9.1Ĭlark, 103, 7.1 Reading CSV Files by Using BufferedReader The contents of the file are shown below. For this tutorial, we will use a simple CSV file that contains just three records.
#How to find word in file in jav how to
In this tutorial, we will learn how to read a CSV file and copy its content into an array or list. Each row of the file is used to represent a data record. CSV files are used to store information delimited by commas. CSV stands for Comma Separated Values and is a very popular file type.
