CSV files opening in Notepad not Excel

CSV stands for Comma-Separated Values. It is a document design utilized to keep information in an accounting page or dataset in a straightforward text structure. A delimiter is used to recognize and isolate information in comma-separated values.

Introduction

The CSV record design moves structured information between programs that locally work on incongruent organizations.

Prerequisites

To follow along, the reader should:

  • Have a basic knowledge of Java.
  • Have an IDE of your choice installed. I’ll be using Eclipse IDE.

Table of contents

  • Introduction
  • Prerequisites
  • Table of contents
  • Creating a CSV file
    • Microsoft Excel office
    • Notepad
  • Java String.split() technique
  • Java scanner class
  • Using openCSV API
    • Important OpenCSV classes
  • Reading CSV files in Java
  • Conclusion

Creating a CSV file

Microsoft Excel office

  • First, open the Excel office on your computer.
  • Second, open a blank workbook. Then feed the information beneath into the Excel document.

  • Save the excel file as CSV (comma delimited). Name the document as comma-separated, and finish saving it.

Notepad

  • Let’s get started by opening Notepad.
  • Second, feed in the information as displayed below. Remember to separate each data with a comma.

Student Name,Faculty,Registration No,Fees Balance,Campus Stanley Kuria,Education,37916808,3150,Main Davidson Kanyua,Computing,38094044,8500,Meru Tess Wambui,Engineering,21666504,13500,Nairobi Esther Njoroge,Health,37809504,5500,Nakuru

  • Then save the file as commaseperated.csv. The file we created should look like the one below.

Java String.split() technique

import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 0, applied to break string around matches of the given standard articulation. In the code below, we imported import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 1. import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 1 reads the file line by line to the end.

import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } }

The output of the code is as shown below:

Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru

Java scanner class

This is a class in the import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 3 bundle, used to get input from the user. It breaks information into tokens, utilizing the delimiter pattern. It is the most straightforward method to read input in Java code.

We used the scanner class to read CSV files in Java in the code below:

import java.util.Scanner;//contains scanner class import java.io.*; class mycsvread { public static void main(String[] args) throws FileNotFoundException //This exception will be thrown when a file with the specified pathname does not exist { Scanner myscanner = new Scanner(new File("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv"));//Creates a new File myscanner.useDelimiter(", "); //Sets the delimiter while (myscanner.hasNext()) //Returns a boolean(true) if this scanner has another token in its input { System.out.println(myscanner.next());//next complete scanner is returned here } myscanner.close(); //myscanner is closed } }

The output is as shown below:

Student Name,Faculty,Registration No,Fees Balance,Campus Stanley Kuria,Education,37916808,3150,Main Davidson Kanyua,Computing,38094044,8500,Meru Tess Wambui,Engineering,21666504,13500,Nairobi Esther Njoroge,Health,37809504,5500,Nakuru

Using openCSV API

OpenCSV is an outsider API utilized to peruse different adaptations of the comma-separated document. It supports all basic Comma-separated operations. OpenCSV is used since Java does not provide native support to read Comma-Separated values.

Important OpenCSV classes

  • import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 4 - enables writing data into a CSV file.
  • import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 5 - utilized to populate Java Application with CSV data.
  • import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 6 - applied to export information from JavaBean to Comma-seperated value file.

Comma-separated values are read in two ways, mainly:

  • By line: In this case, the comma-separated values are consecutively read from one line to another.
  • All data: utilizes import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 7 technique to peruse all records at once.

import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 8

The syntax for open CSV: import java.io.FileReader; import java.io.IOException;//signals an exception of some kind has occurred import java.io.BufferedReader;//Reads text from a character-input stream class readcsv { public static void main(String[] args) { String sample = ","; String mystring; try { BufferedReader brdrd = new BufferedReader(new FileReader("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv")); while ((mystring = brdrd.readLine()) != null) //Reads a line of text { String[] student = mystring.split(sample);//utilized to split the string System.out.println("Name: " + student[0] + ",Faculty: " + student[1] + ", Registration No: " + student[2] + ", Fees Balance: " + student[3] + ", Campus: " + student[4] +""); } } catch (IOException e)//catches exception in the try block { e.printStackTrace();//Prints this throwable and its backtrace } } } 9

Reading CSV files in Java

Step 1

Create two folders in eclipse-workspace and name them Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 0 and Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 1. In the Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 1 folder, move the CSV files that you will use. In this case, we will use comma-separated and semicolon-separated files.

Then in the Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 0 folder paste Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 4 and Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 5 jar files. If you don’t have them, download them by clicking opencsv5.5.2 and commonlangs3-3.1

Step 2

Open Eclipse, and create a Java project. Name the project as Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 6 and click next.

Step 3

Click Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 7 and select Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 8. Then select Name: Student Name,Faculty: Faculty, Registration: Registration No, Fees Balance: Fees Balance, Campus: Campus Name: Stanley Kuria,Faculty: Education, Registration: 37916808, Fees Balance: 3150, Campus: Main Name: Davidson Kanyua,Faculty: Computing, Registration: 38094044, Fees Balance: 8500, Campus: Meru Name: Tess Wambui,Faculty: Engineering, Registration: 21666504, Fees Balance: 13500, Campus: Nairobi Name: Esther Njoroge,Faculty: Health, Registration: 37809504, Fees Balance: 5500, Campus: Nakuru 9 and add jars from CSVDoc in the Eclipse-workspace folder. Then click finish.

Step 4

After that, create a Java class. Right-click on the project CSVOperation and select new, then select class.

Step 5

Name the class as import java.util.Scanner;//contains scanner class import java.io.*; class mycsvread { public static void main(String[] args) throws FileNotFoundException //This exception will be thrown when a file with the specified pathname does not exist { Scanner myscanner = new Scanner(new File("C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv"));//Creates a new File myscanner.useDelimiter(", "); //Sets the delimiter while (myscanner.hasNext()) //Returns a boolean(true) if this scanner has another token in its input { System.out.println(myscanner.next());//next complete scanner is returned here } myscanner.close(); //myscanner is closed } } 0 and click finish.

Step 6

Then copy the code sample beneath and paste. Run the program.

import com.opencsv.CSVReader;//external library import java.io.FileReader; //Reads text from character files using a default buffer size public class ReadCSVExample { private static String LOCATION_OF_THE_FILE="C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\commaseperated.csv";//file location public static void main(String[] args) { LineByLine(LOCATION_OF_THE_FILE); } public static void LineByLine(String myfile) { try { FileReader freader= new FileReader(myfile);//created an object of freader class @SuppressWarnings("resource") CSVReader creader= new CSVReader(freader);// created creader object by parsing freader as a parameter String [] nextRecord;// created an array of type String //read data line by line while((nextRecord = creader.readNext())!=null) { for(String token: nextRecord) System.out.println(token +"\t"); //will bring the value of cell seperated by tab space } System.out.println(); } catch(Exception e) //to catch any exception inside try block { e.printStackTrace();//used to print a throwable class along with other dataset class } } }

The output should resemble the one below:

Student Name Faculty Registration No Fees Balance Campus Stanley Kuria Education 37916808 3150 Main Davidson Kanyua Computing 38094044 8500 Meru Tess Wambui Engineering 21666504 13500 Nairobi Esther Njoroge Health 37809504 5500 Nakuru

You will note the absence of commas. This is because when using open CSV API, the commas are ignored.

Let’s implement a program to read a CSV file not separated by commas. Instead, it’s separated with semi-colons(;).

The CSV file that we are going to read is shown below:

Copy the program below and paste and run it.

import com.opencsv.CSVReader;//external library import java.io.FileReader; //Reads text from character files using a default buffer size public class ReadCSVExample { private static String LOCATION_OF_THE_FILE="C:\\Users\\davis\\eclipse-workspace\\CSVdocuments\\semicolonseperated.csv";//file location public static void main(String[] args) { LineByLine(LOCATION_OF_THE_FILE); } public static void LineByLine(String myfile) { try { FileReader freader= new FileReader(myfile);//created an object of freader class @SuppressWarnings("resource") CSVReader creader= new CSVReader(freader);// created creader object by parsing freader as a parameter String [] nextRecord;// created an array of type String //read data line by line while((nextRecord = creader.readNext())!=null) { for(String token: nextRecord) System.out.println(token +"\t"); //will bring the value of cell seperated by tab space } System.out.println(); } catch(Exception e) //to catch any exception inside try block { e.printStackTrace();//used to print a throwable class along with other dataset class } } }

The output is shown below:

Student Name;Faculty;Registration No;Fees Balance;Campus Stanley Kuria;Education;37916808;3150;Main Davidson Kanyua;Computing;38094044;8500;Meru Tess Wambui;Engineering;21666504;13500;Nairobi Esther Njoroge;Health;37809504;5500;Nakuru

In the output above, you will note the presence of semi-colon separators. This is because OpenCSV ignores commas, but other separators are recognized.

Conclusion

This article focused on various techniques to create CSV files in Java. We also learned different methods of reading Comma Separated Values (CSV) in Java. Finally, we used open CSV API to read CSV files.

Postingan terbaru

LIHAT SEMUA