CsvToBean parse csv file java - opencsv

public static <T> List<T> getCsvData(String file, Class<T> clazz) throws IOException {
try (FileReader fr = new FileReader(file)) {
HeaderColumnNameMappingStrategy<T> strategy = new HeaderColumnNameMappingStrategy<>();
strategy.setType(clazz);
CsvToBean<T> csvToBean = new CsvToBeanBuilder<T>(fr)
.withSeparator(';')
.withQuoteChar('\'')
.withIgnoreQuotations(true)
.withMappingStrategy(strategy).build();
return csvToBean.parse();
}
}
I use CsvToBean to convert a csv file into a bean. The csv file uses a semicolon as a separator. When a column value in the file contains a semicolon, it will fail to parse. Is there any way to solve it?
elample 'aa;a';'bbb';

Related

JavaFX - tableview shows data but when saving to file, the file is empty

In my program I read a config file (.txt file) and show the content in a tableview. That works.
Tableview with data
The second column can be edited. This serves as a config file for the program.
If I now save the data (File - Save) the config file is empty. I have no idea, why. Here is the code:
File - Save calls this:
#FXML
public void saveConfig() throws IOException {
System.out.println("File - Save clicked");
SCDConfigDataAccess configData = new SCDConfigDataAccess();
configData.saveData(SCDController.configFile);
closeConfig();
}//saveConfig
and configData.saveData does this:
public class SCDConfigDataAccess {
private static ObservableList<SCDConfigData> scdConfig;
public void saveData(File configFile) throws IOException {
BufferedWriter bw = Files.newBufferedWriter(Paths.get(configFile.getPath()));
String output = "";
System.out.println("File: " + configFile.getPath());
try {
for (SCDConfigData data : scdConfig) {
output = data.getsConfigType() + "=" + data.getsConfigValue() + "\n";
System.out.println("Data: " + output);
bw.write(output); }
bw.flush();
bw.close();
}catch(IOException e){ System.out.println("Error: " + e.getMessage()); }
} //saveData
}//class
I get these messages:
File - Save clicked
File: C:\Users\Michael\AppData\Local\SCD\scdconfig.ini
These are expected and correct.
I do not get a message out of the for-loop. That lets me think there is no data. But why? I see the data.
I'm sure I'm just missing a little thing. Any help is appreciated.
The config file was 127 bytes when I read it and is now 0.
Thanks,
Michael
I figured it out. The problem was this line of code in the method saveConfig() of the controller
SCDConfigDataAccess configData = new SCDConfigDataAccess();
This creates a new instance and this does not have a connection to the data. Instead of doing this I use the instance defined in the controller class itself like so:
public class SCDConfigController {
private SCDConfigDataAccess configDataAccess;
public void saveConfig() throws IOException {
configDataAccess.saveData(SCDController.configFile);
closeConfig();
}//saveConfig
And this of course works.

With OpenCSV, how do I append to existing CSV using a MappingStrategy?

With OpenCSV, how do I append to existing CSV using a MappingStrategy? There are lots of examples I could find where NOT using a Bean mapping stategy BUT I like the dynamic nature of the column mapping with bean strategy and would like to get it working this way. Here is my code, which just rewrites the single line to CSV file instead of appending.
How can I fix this? Using OpenCSV 4.5 . Note: I set my FileWriter for append=true . This scenario is not working as I expected. Re-running this method simply results in over-writing the entire file with a header and a single row.
public void addRowToCSV(PerfMetric rowData) {
File file = new File(PerfTestMetric.CSV_FILE_PATH);
try {
CSVWriter writer = new CSVWriter(new FileWriter(file, true));
CustomCSVMappingStrategy<PerfMetric> mappingStrategy
= new CustomCSVMappingStrategy<>();
mappingStrategy.setType(PerfMetric.class);
StatefulBeanToCsv<PerfMetric> beanToCsv
= new StatefulBeanToCsvBuilder<PerfMetric>(writer)
.withMappingStrategy(mappingStrategy)
.withSeparator(',')
.withApplyQuotesToAll(false)
.build();
try {
beanToCsv.write(rowData);
} catch (CsvDataTypeMismatchException e) {
e.printStackTrace();
} catch (CsvRequiredFieldEmptyException e) {
e.printStackTrace();
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Or, is the usual pattern to load all rows into a List and then re-write entire file? I was able to get it to work by writing two MappingStrategy mapping strategies and then conditionally using them with a if-file-exists but doing it that way leaves me with a "Unchecked assignment" warning in my code. Not ideal; hoping for an elegant solution?
I've updated OpenCSV to version 5.1 and got it working. In my case I needed the CSV headers to have a specific name and position, so I'm using both #CsvBindByName and #CsvBindByPosition, and needed to create a custom MappingStrategy to get it working.
Later, I needed to edit the MappingStrategy to enable appending, so when it's in Appending mode I don't need to generate a CSV header.
public class CustomMappingStrategy<T> extends ColumnPositionMappingStrategy<T> {
private boolean useHeader=true;
public CustomMappingStrategy(){
}
public CustomMappingStrategy(boolean useHeader) {
this.useHeader = useHeader;
}
#Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
final int numColumns = FieldUtils.getAllFields(bean.getClass()).length;
super.setColumnMapping(new String[numColumns]);
if (numColumns == -1) {
return super.generateHeader(bean);
}
String[] header = new String[numColumns];
if(!useHeader){
return ArrayUtils.EMPTY_STRING_ARRAY;
}
BeanField<T, Integer> beanField;
for (int i = 0; i < numColumns; i++){
beanField = findField(i);
String columnHeaderName = extractHeaderName(beanField);
header[i] = columnHeaderName;
}
return header;
}
private String extractHeaderName(final BeanField<T, Integer> beanField){
if (beanField == null || beanField.getField() == null || beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0){
return StringUtils.EMPTY;
}
//return value of CsvBindByName annotation
final CsvBindByName bindByNameAnnotation = beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class)[0];
return bindByNameAnnotation.column();
}
}
Now if you use the default constructor it'll add the header to the generated CSV, and using a boolean you can tell it to add a header or to ignore it.
I never found an answer to this question and so what I ended up doing was doing a branch if-condition where .csv file exists or not. If file exists I used MappingStrategyWithoutHeader strategy, and if file didn't yet exist, I used MappingStrategyWithHeader strategy. Not ideal, but I got it working.

How to avoid extent report to not to overwrite the html file name

I am using extent reports in appium with testng and its working fine for me.whenver my tests run is completed then extent report generates html file in my project folder and that is what expected.
Issue is that when I again run my tests then extent report generate new html report file by overwrting the name of previously created html file.
I want extent report to generate html file with unique names or name with date in in, each time when I run my tests
You can create your file name to be the current timestamp. This way, it will be easy to have a unique name for your report file -
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
extent = new ExtentReports (userDir +"\\test-output\\" + timeStamp + ".html", true);
You can do it by setting unique name:
String reportFile = resultDirectory + fileName + ".html";
than method for saving report to certain folder:
public void saveReportFolder() throws IOException {
File srcDir = new
File(System.getProperty("user.home")+"/Automation/target");
File destDir = new File(System.getProperty("user.home") + "/reports/"+ System.getProperty("user.name")+"/"+dateTimeGenerator());
FileUtils.copyDirectory(srcDir, destDir);
}
...and utility for setting dateTime:
public static String dateTimeGenerate(){
Format formatter = new SimpleDateFormat("YYYYMMdd_HHmmssSSS");
Date date = new Date(System.currentTimeMillis());
return formatter.format(date);
}
Or simply use klov reports start server and have everything in database (MongoDb), it is more elegant way to go.
Hope this helps,
I use:
private static String timestamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()).replaceAll(":", "-");
public static String reportFullPath = getReportsPath() + "\\AutomationReport_" + timestamp + ".html";
I have done it like this, simple and crisp.
String Outputfilename= ExecutionConfig.FileOutname;
System.err.close(); // written to remove JAVA 9 incompatibility.. continued below
System.setErr(System.out); // continue.. and remove the warnings
extent = new ExtentReports(System.getProperty("user.dir") + "/test-output/"+Outputfilename+".html", true);
So here ExecutionConfig.FileOutname is called from the class ExecutionConfig where i am reading the values from the config.properties file. and then here assigning it to the output-file.
Also it worked for me.
I also faced a similar issue. As in the real-world, we need old reports as well. Below is the solution in Java for Extent PDF report
I added an event listener method. Event used- TestRunStarted. We further need to register for this event too. The solution can be done for HTML report too.
public void setCustomReportName(TestRunStarted event)
{
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String currenttimestamp =sdf.format(timestamp);
Properties prop=new Properties();
//extent.reporter.pdf.out is the name of property which tell the report path
prop.setProperty("extent.reporter.pdf.out", "test output/PdfReport/ExtentPdf_"+currenttimestamp+".pdf");
ExtentService e1 =new ExtentService();
//ExtentReportsLoader is the inner class of ExtentService and initPdf is its private method which takes the path for report
Class<?>[] a=e1.getClass().getDeclaredClasses();
Method met;
//Even there is exception test run wont fail and report will also be generated (ExtentPdf.pdf)
try {
met = a[0].getDeclaredMethod("initPdf", Properties.class);
met.setAccessible(true);
met.invoke(a[0], prop);
} catch (NoSuchMethodException e) {
System.out.println("There is no method with name initPdf");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println("Argument passed to method initPdf are not correct");
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

How to load Resx Files under App_LocalResources to Iterate through it

I need the ResX file for the current page which is saved under "App_LocalResources".
I need to iterate through it.
The Method "GetLocalResourceObject" only allows access to one key at the time.
Since a resx file is nothing more than a xml file, you can loop all the elements with XElement
string resxFile = Server.MapPath("/App_LocalResources/Default.aspx.resx");
foreach (XElement element in XElement.Load(resxFile).Elements("data"))
{
string currentItem = string.Format("Key: {0} Value: {1}", element.Attribute("name").Value, element.Element("value").Value);
}
Another option is with the ResXResourceReader. However you do need to add System.Windows.Forms as a reference to the project.
using System.Resources;
//define the filename and path for the resx file
string resxFile = Server.MapPath("/App_LocalResources/Default.aspx.resx");
//load the file into the reader
using (ResXResourceReader reader = new ResXResourceReader(resxFile))
{
//loop all the entries
foreach (DictionaryEntry entry in reader)
{
string currentItem = string.Format("Key: {0} Value: {1}", entry.Key, entry.Value);
}
}

Tree map not sorting in order

So my code so far reads in lines from a file, stores them in two separate ArrayLists. Now I'm going to have to make each node in a doubly Linked List contain the line's string and the corresponding integer, but since I cant put two ArrayLists in the declaration for a Linked List, I tried making a Tree map where the key is the string and the value is the corresponding integer. Here's the relevant code
public class FileReaderProgram
{
public static void main(String[] args) throws IOException
{
ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
String stringComponent = " ";
int integerComponent = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the absolute path of the file");
String fileName = in.next(); //Gets the file from the user
File inFile = new File(fileName);
**Map<String,Integer> lineCombo = new TreeMap<String,Integer>();**
try
{
Scanner fileReader = new Scanner(inFile); //Constructs Scanner for reading the file
fileReader.useDelimiter("\\n");
while (fileReader.hasNextLine())
{
String line = fileReader.nextLine(); //Gets line from the file
Scanner lineScanner = new Scanner(line); //Constructs new scanner to analyize the line
lineScanner.useDelimiter(",");
stringComponent = lineScanner.next(); //Read first word
while (!lineScanner.hasNextInt())
{
stringComponent = stringComponent + " " + lineScanner.next(); //Checks if more than one word part of string
}
integerComponent = lineScanner.nextInt(); //Read in integer
words.add(stringComponent); //Array of Strings, element number corresponding to the line it came from
numbers.add(integerComponent); //Array of Ints, same thing as above
**lineCombo.put(stringComponent, integerComponent);**
}
}
So when I run the file:
example string,1
another example,42
data,200
final,150
it prints out {another example=42, data=200, example string=1, final=150}
Why is it putting the strings in the wrong order?

Resources