All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cookercucumber_parser.flatFileParser.CSVParser Maven / Gradle / Ivy

Go to download

Derives smallest Feature File, Allows Data from Excel(xls and xlsx) and Also provides a clear and concise reporting

There is a newer version: 3.1.0
Show newest version
package cookercucumber_parser.flatFileParser;


import org.apache.commons.io.FilenameUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVParser {

    /**
     * Read the CSV File and Convert that into Examples Format for Scenario Outline
     * 
Author : Manjunath Prabhakar ([email protected])
* * @param filePath Folder Path Only * @param fileName FileName with Extension (csv) * @param delimiter delimiter inside the csv * @return CSV Data in Examples Format * @throws IOException If Any */ public static String readCSV(String filePath, String fileName, String delimiter) throws Exception { StringBuilder exampleFromCSV = new StringBuilder(); exampleFromCSV.append("Examples:"); exampleFromCSV.append(System.getProperty("line.separator")); //Create an object of File class to open csv file File file = new File(filePath + "\\" + fileName); List acceptedDelimiters = new ArrayList<>(); acceptedDelimiters.add(","); acceptedDelimiters.add(";"); acceptedDelimiters.add("|"); if (!FilenameUtils.getExtension(fileName).equalsIgnoreCase("csv")) { throw new Exception("Invalid Extension. Accepted(csv)"); } if (!(acceptedDelimiters.contains(delimiter))) { throw new Exception("Invalid Delimiters. Accepted(,(comma) or ;(semi-colon) or |(pipe) )"); } FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String line = ""; String[] tempArr; while ((line = bufferedReader.readLine()) != null) { tempArr = line.split(delimiter); exampleFromCSV.append("|"); for (String tempStr : tempArr) { exampleFromCSV.append(tempStr); exampleFromCSV.append("|"); } exampleFromCSV.append(System.getProperty("line.separator")); } bufferedReader.close(); fileReader.close(); return exampleFromCSV.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy