How to Read Write Object’s Data in CSV Format Using Notepad in Java?
The CSV stands for Comma-Separated Values. CSV files can be used with almost any spreadsheet program, such as Microsoft Excel or Google Spreadsheets. They differ from other spreadsheet file types because you can only have a single sheet in a file, they can not save cell, column, or row. Also, you cannot save formulas in this format.
Now, to store or to write the data member’s value of an object in a fixed format implies that a record of a fixed length in a file, we can use the combination of FileWriter, BufferedWriter, and String.format() where the String.format() method returns the formatted string by a given locale, format, and argument. The java string format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
A. File Reading
- Open a file (using the fully qualified name of the file) using FileReader & BufferedReader.
- Use the split() method to split the data that was read in comma–separated format.
- Read the individual split values.
- Print the values.
- Close both the Readers.
B. File Writing
- Create any class Object & assign values to its data members.
- Create & open a file (using the fully qualified name of the file) in append mode using FileWriter & BufferedWriter.
- Use the String.format() method to create a string of a fixed format containing the values of data members of the object to be written. (The format should contain “,” to store the string in CSV format)
- Write the formatted string to the file using write() method of BufferedWriter Class.
- Close both the Writers.
Implementation: Here we are considering a Customer class having 2 data members namely ‘name’ and ‘account_No’. While reading we need to read the data that was written in CSV format in the file & split that data read. While writing we need to save the value of each data member for every object of Customer type (in CSV format ) in a file named “CustomerData.txt”. We are creating a String format that contains 30 bytes of file for Customer’s name & 10 bytes for Customer’s account_No (to save every object’s data members in the same format) using format() method of String class. For example:
String.format("%30s ,%10d",name, acc_No);
Note: replaceAll() method is used to remove all the white spaces (//s in Unicode) from the data read.
Output: Two different output are obtained
- Output 1: When we run for the first time
- Output 2:: When the program is run again after the first time
Output 1: When we run for the first time
Menu 1. For Creating New Customer and saving it to the file 2. For Viewing File 3. For Exitn Enter Your Choice 1 Enter Customer's Name Ramesh Successfully wrote to the file. Wanna Work More??? Yes/No Y Enter Your Choice 1 Enter Customer's Name Rohan Successfully wrote to the file. Wanna Work More??? Yes/No y Enter Your Choice 2 Customers are : Name : Ramesh And account no is : 100 Name : Rohan And account no is : 101 Wanna Work More??? Yes/No N
Also, after execution, “CustomerData.txt” was created & it contains the following contents :
Ramesh , 100 Rohan , 101
As you can see above 30 bytes have been reserved for the name (Eg: 24 white spaces & 6 bytes for Ramesh) and 10 bytes for the account no for each line written because we used String.format() for doing so.
When you read these lines one by one as a string, that string will contain white spaces also, to remove that white spaces we used the replaceAll() method to get a new string with no white spaces.
Eg: First line when read, str = " Ramesh, 100"
When we use replaceAll(), str becomes
str = “Ramesh,100”
The string has no white spaces now has comma-separated values. In order to fetch those values individually, we used split() method & fetched the individual values & printed them.
Eg : After splitting the str according to "," in our example we will had: splitString = Ramesh & splitString = 100
So, the output was printed according to these split values
Output 2: When the program is run again after the first time
Menu 1. For Creating New Customer And saving it to the file 2. For Viewing File 3. For Exitn Enter Your Choice 1 Enter Customer's Name Chetan Successfully wrote to the file. Wanna Work More??? Yes/No y Enter Your Choice 2 Customers are : Name : Ramesh And account no is : 100 Name : Rohan And account no is : 101 Name : Chetan And account no is : 100 Wanna Work More??? Yes/No N
Now, before executing for the second time, you were already having the file “CustomerData.txt”’ that contains 2 lines of data.
Ramesh , 100 Rohan , 101
When you write the data in the append mode, the new data get to write after the data that was already there in the file. So the file “CustomerData.txt” would appear as shown below:
Ramesh , 100 Rohan , 101 Chetan , 100
The file is then read in the same way as earlier & in the same way, all the contents of the file are read line by line, white spaces are removed & splitting is done & finally split data is printed.