Posts

Showing posts from May, 2016

Generalized Apache POI script for reading and writing to .xlsx files

In this post, we will be looking to simplify the script mentioned in this post . Below script is generalized version so it can be used by other classes for reading and writing to .xlsx files. The code for reading and writing to .xlsx is made modular by putting them in different class and making read and write methods static. Any test that needs to read from excel file can just call the readFromXLSX() method and similarly any test that needs to write data to excel file, can just call the writeTestResultToXLSX() method and passing the appropriate parameters to it. Here's the class that contains the read and write methods: Below code makes use of Enumerations to restrict the value of test result to be either PASS or FAIL. This would make TestNG tests that will use these methods, not to write illegal values into the excel sheet.  import java.io.File ; import java.io.FileInputStream ; import java.io.FileNotFoundException ; import java.io.FileOutputStream ; import java.

Reading and writting data from .xlsx spreadsheet using Apache POI API

Image
Why to use Apache POI and not Jexcel API ? Below are the drawbacks of using Jexcel API for interacting with excel : Jexcel supports .xls format of MS excel, which is not the default option we get while saving excel sheets, nowadays. So most of the excel sheets are by default get saved in .xlsx format. Apache POI supports not only .xls but also .xlsx type of spreadsheets. We normally come across scenario such as - we want to read test data from the excel sheet and then write back to same excel sheet with some other related information. This can be achieved using Apache POI and not by Jexcel API as Jexcel does not support modifying a spreadsheet that is being used for reading. Jexcel API is no longer in development i.e. no future updates/enhancements are not possible but Apache POI is still in development and updates/enhancements are very much possible. So, in the long run, it'd be beneficial to use Apache POI in code rather than Jexcel API. Let's see a simple script t

How to Run Windows Commands in Java and Store Their Output in Log File

In our day to day work we need to run windows commands and verify their output. In order to automate these frequently used commands, we normally write them in batch file but this option does not produce log like file and does not have good file formatting features.To solve this problem, below given utility program can serve just the right purpose. Here we make use of java.io class to write output file and make use of java's Runtime class methods to execute windows commands. Below is the code, that will run windows commands and store their output in a .txt file with formatting and date of run of commands. Complete code is available at my GitHub Project to change it to suite your needs. import java.io.BufferedReader ; import java.io.BufferedWriter ; import java.io.File ; import java.io.FileWriter ; import java.io.IOException ; import java.io.InputStreamReader ; import java.util.Arrays ; import java.util.Date ; public class RunningWinCmd { public st

Copy contents of a file to another using Java

Here’s a small utility to copy contents of one file to another using java programming. We make use of java.io package present in JDK. This class provides rich set of I/O streams for reading and writing to files.  For copying contents of one file to another, we will make use of FileReader and FileWriter classes of java.io package. Since methods provided by these classes are low-level i.e they work on single characters or stream of characters, these classes need to wrapped with higher-level classes such as BufferedReader for FileReader and BufferedWriter for FileWriter. These high-level classes are efficient as they make use of buffer and work on large chuck of data at a time. Below is the code, that will copy contents of one file to another. Userinput.txt is the source file and useroutput.txt is destination file. Complete code is available at my GitHub Project. import java.io.BufferedReader ; import java.io.BufferedWriter ; import java.io.File ; import java.io.F