title: I want to read two files in Java, one .xlsx and one .csv, and want to display the contents on the console based on the extension of the file
body: <p>I want to read two files in Java, one <code>.xlsx</code> and one <code>.csv</code>, and want to display the contents on the console based on the extension of the file. I've stored the extension in <code>ext1</code> and <code>ext2</code>, but I'm not getting how do I display the contents of the file based on the extension.</p>
  
  <pre><code>package com.pack;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.Iterator;
  import java.util.Scanner;
  
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  import org.apache.commons.io.FilenameUtils;
  
  public class TP {
  
      public static void main(String[] args) throws FileNotFoundException {
  
          try {
              String ext1 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx"); // returns "txt"
              String ext2 = FilenameUtils.getExtension("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input2.csv"); // returns "exe"
              String input = null;
              String excelPath = "C:\\Users\\swapnil.sanjay.saraf\\Desktop\\Input.xlsx";
              if (excelPath == ext1)
  
              {
  
                  FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
  
                  // Create Workbook instance holding .xls file
                  XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
  
                  // Get the first worksheet
                  XSSFSheet sheet = workbook.getSheetAt(0);
  
                  // Iterate through each rows
                  Iterator &lt; Row &gt; rowIterator = sheet.iterator();
                  System.out.println("\n******XLSX FILE******\n");
                  while (rowIterator.hasNext()) {
                      // Get Each Row
                      Row row = rowIterator.next();
  
                      // Iterating through Each column of Each Row
                      Iterator &lt; Cell &gt; cellIterator = row.cellIterator();
  
                      while (cellIterator.hasNext()) {
                          Cell cell = cellIterator.next();
  
                          // Checking the cell format
                          switch (cell.getCellType()) {
                              case Cell.CELL_TYPE_NUMERIC:
                                  System.out.print(cell.getNumericCellValue() + "\t");
                                  break;
                              case Cell.CELL_TYPE_STRING:
                                  System.out.print(cell.getStringCellValue() + "\t");
                                  break;
                              case Cell.CELL_TYPE_BOOLEAN:
                                  System.out.print(cell.getBooleanCellValue() + "\t");
                                  break;
                          }
                      }
                      System.out.println(" ");
                  }
                  System.out.println(ext1);
              }
          } catch (IOException ie) {
              ie.printStackTrace();
          }
      }
  }
  </code></pre>