Extracting/Reading the CSV file and TXT file and Writing the data into CSV file


Exacting the CSV file.

String exactedFile = "D:\\Temp\\test.csv";


CSVReader csvReader = new CSVReader(new FileReader(exactedFile));  //Reading the CSV file.

String[] row = null;
while((row = csvReader.readNext()) != null) { // Reading the individual line
                         
                                 row[0] // printing the first cell.
                                 row[1] // printing the second cell.
                          }

We need to add opencsv-1.7.jar in the class path.

import au.com.bytecode.opencsv.CSVReader;

Reading the TXT file.


final String file1 = "D:\\Temp\\creds.txt";
BufferedReader br = null;
String sCurrentLine;

try {
             br = new BufferedReader(new FileReader(file1)); // Reading the TXT file.

while ((sCurrentLine = br.readLine()) != null) { // Reading the individual line.
                                 System.out.println("sCurrentLine"+sCurrentLine ); // Printing the line.
                        }

} catch(Exception e ) {

}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


Writing the CSV file

final String outputFile = "D:\\Temp\\finaloutput.csv";


CSVWriter writer = new CSVWriter(new FileWriter(outputFile)); // Creating the stream to insert the data
 List<String[]> data = new ArrayList<String[]>();

data.add(new String [] {"liferay", "open" , "source"}); // inserting the data


writer.writeAll(data); // writing the data into the CSV file
writer.close();


We need to add opencsv-1.7.jar in the class path.

import au.com.bytecode.opencsv.CSVWriter;






Comments

Post a Comment

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate