How to Convert CSV to TXT in Java? – Easy Method[2023]

In this post, we will see how to convert CSV to TXT in Java. We will convert the CSV file to an Array and then we will convert that Array to a TXT file.

Simple Logic:

To read data from the CSV file and write data to the TXT file, we have used the File I/O concept of Java. To convert CSV to TXT file, first, we need to read data line by line from CSV file and then we can convert line by line to a TXT file.

We have taken a sample CSV file to convert to TXT file. This file contains data of employees of different companies like Employee Name, Company Name, Designation and Salary.

See below data of CSV file to get details.

SL NONAMECOMPANYDESIGNATIONSALARY
1SATYAJEET NAYAKABCSOFTWARE ENGINEER70000
2MACK MOHANDEFHR60000
3JACK JOOGHIADMIN90000
4MARLON MACJKLCEO256000
5NOOR MAHOTAMNOTESTER40000

You can also use the below CSV file to convert to a TXT file in Java. To use this CSV file, just download this file from below and save it to your system. You can use this file location of your System as the path in the program.

How to convert CSV to TXT in Java?

You can convert CSV to TXT in Java by using the File IO concept. We can use FileReader and BufferedReader classes to read data from the CSV file and then FileWriter class to write the same data to a TXT file.

Let’s see the program now.

/**
 * How to Convert CSV to TXT in Java?
 */
package cf.java.logical;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class ReadCSVFileAndConvertToTXTFile {

	public static void main(String[] args) throws Exception {
		// Declare CSV file path as a String
		String path = "C:/Satyajeet Nayak/Codingface/Demo.csv";
		// Use File
		File file = new File(path);
		// Use FileReader to red CSV file
		FileReader fr = new FileReader(file);
		// User BufferReader
		BufferedReader br = new BufferedReader(fr);
		String line = "";

		String[] tempArr;
		// User FileWriter to write content to text file
		FileWriter writer = new FileWriter("output.txt");
		// Use while loop to check when file contains data
		while ((line = br.readLine()) != null) {
			tempArr = line.split(",");
			// User for loop to iterate String Array and write data to text file
			for (String str : tempArr) {
				writer.write(str + " ");
			}
			// Write each line of CSV file to multiple lines
			writer.write("\n");
		}
		writer.close();

	}

}Code language: JavaScript (javascript)

Output(TXT File):

SL NO NAME COMPANY DESIGNATION SALARY 
1 SATYAJEET NAYAK ABC SOFTWARE ENGINEER 70000 
2 MACK MOHAN DEF HR 60000 
3 JACK JOO GHI ADMIN 90000 
4 MARLON MAC JKL CEO 256000 
5 NOOR MAHOTA MNO TESTER 40000 

Explanation:

  • First, we have declared the file path of the CSV file as a String(String path = “C:/Satyajeet Nayak/Codingface/Demo.csv”;).
  • We have use File, FileReader, and BufferedReader classes to read data from CSV files.
  • We have used a demo TXT file(output.txt) to store employee data from the CSV file.
  • Then, we have used a while loop(while ((line = br.readLine()) != null)) to check that file should not be empty. When the file is not empty, by using the write( ) method of Writer class we can write each line of CSV file to TXT file.
  • Finally, the whole data of the CSV file will be converted to a TXT file.

Also Read: Palindrome In Java(String & Number) – Check 2 Easy Methods

Notepad Image:

Convert CSV to TXT in Java output file

Other Programmers Also Ask For:

  1. How to convert CSV file to TXT file in Java?
  2. How to convert CSV to TXT file?
  3. How to generate a TXT file from CSV file?

Our Recommended Programs:

  1. How To Find Multiples Of A Float Value In Java?
  2. How To Find Power Of A Number In Java – 3 Simple Ways
  3. Palindrome In Java(String & Number) – Check 2 Easy Methods
  4. How To Check String Contains Special Characters In Java?
  5. Java Program To Print Vowels In A String – 2 Simple Programs

3 thoughts on “How to Convert CSV to TXT in Java? – Easy Method[2023]”

Leave a Comment