How to Read/convert an InputStream to a String

There are multiple ways .I will list down few of them.

Using Old and standard java solution –

public static String fromStream(InputStream in) throws IOException
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
    return out.toString();

return sb.toString();

if you using Google-Collections/Guava-

InputStream stream = ...
String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));

If you can use Apache Common library..then this is useful –

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

Quick way but only work in deserialization process –

String result = (String)new ObjectInputStream( inputStream ).readObject();

Note:ObjectInputStream is about deserialization, and the stream have to respect the serialization protocol to work, which may not always true in all cases.

In the end, the most efficient solution and only in two lines using java Scanner class –

Tricky is to remember the regex \A, which matches the beginning of input. This effectively tells Scanner to tokenize the entire stream, from beginning to (illogical) next beginning.

public static String convertToString(InputStream in) {
    java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");  
    return s.hasNext() ? : "";

I would love to hear any comment by you for making more simple…

Happy learning by Vinay Kumar in techartifact

Read Excel file using Apache POI in Java

Requirment – How to read an excel file in Java

Solutions– Well Apache POI is answer.This API will help you a have to download and add the POI JAR file to your project’s class path. The Apache POI JAR file can be found
Please note

HSSF (Horrible SpreadSheet Format) – reads and writes Microsoft Excel (XLS) format files.
XSSF (XML SpreadSheet Format) – reads and writes Office Open XML (XLSX) format files.

Following snippet we will use

FileInputStream file = new FileInputStream(new File("C:\\VinayTest.xls"));
//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();

Following is code to read from excel file

public static void main(String[] args) throws IOException {
        // Location of the source file
        String sourceFilePath = "C:/Vinay/ApachePoi/TestFile.xls";
        FileInputStream fileInputStream = null;
        // Array List to store the excel sheet data
        List excelData = new ArrayList();
        try {
            // FileInputStream to read the excel file
            fileInputStream = new FileInputStream(sourceFilePath);
            // Create an excel workbook
            HSSFWorkbook excelWorkBook = new HSSFWorkbook(fileInputStream);
            // Retrieve the first sheet of the workbook.
            HSSFSheet excelSheet = excelWorkBook.getSheetAt(0);
            // Iterate through the sheet rows and cells. 
            // Store the retrieved data in an arrayList
            Iterator rows = excelSheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow);
                Iterator cells = row.cellIterator();
                List cellData = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell);
                excelData .add(cellData);
            // Print retrieved data to the console
            for (int rowNum = 0; rowNum < excelData.size(); rowNum++) {
                List list = (List) excelData.get(rowNum);
                for (int cellNum = 0; cellNum < list.size(); cellNum++) {
                    HSSFCell cell = (HSSFCell) list.get(cellNum);
                    if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                        System.out.print(cell.getRichStringCellValue().getString() + " ");
                    } else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                        System.out.print(cell.getNumericCellValue() + " ");
                    } else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                        System.out.println(cell.getBooleanCellValue() + " ");
        } catch (IOException e) {
        } finally {
            if (fileInputStream != null) {

Happy coding with Vinay in techartifact