Skip to main content

How To Read Excel and Text File In Java Code

How To Read Excel and Text File In Java Code


java


Read Excel file in java

Excel is the very popular file format created by Microsoft. 

In this article i have discussing how to read Excel files using java code.


Java Code :

 public static void main(String[] args) throws IOException
 {

        String fileName = "C:\\file-path\\report.xls";
        String cellContent = "Total";
        int rownr=0, colnr = 10;

        InputStream input = new FileInputStream(fileName);

        HSSFWorkbook wb = new HSSFWorkbook(input);
        HSSFSheet sheet = wb.getSheetAt(0);

        rownr = findRow(sheet, cellContent);
        finish();
}

private static int findRow(HSSFSheet sheet, String cellContent)
{
    for (Row row : sheet) {
        for (Cell cell : row)
        {
            Iterator cells = row.cellIterator ();

                while (cells.hasNext ())
                {
                HSSFCell cell = cells.next ();

                switch (cell.getCellType ())
                {
                    case HSSFCell.CELL_TYPE_NUMERIC :
                    {
                        System.out.println ("Numeric value: " + cell.getNumericCellValue ());
                        break;
                    }
                    case HSSFCell.CELL_TYPE_STRING :
                    {
                        HSSFRichTextString richTextString = cell.getRichStringCellValue ();
                        System.out.println ("String value: " + richTextString.getString ());
                        break;
                    }
                    default :
                    {
                        System.out.println ("Type not supported.");
                        break;
                    }
                }       
                        }
                    }              
                    return 0;
}
}


Read Text file in java

Text is the very popular file format and The first notepad Text file was originally invented on 18th February, 1902, when a man named J. A. Birchall created the Silver City Writing Tablet for his stationery company in Tasmania, Australia.

In this article i have discussing how to read Text files using java code.

Simple Steps :

1) First step is very simple only import BufferReader and FileReader Input/Output java package in your class.

2) Create a BufferedReader object with your Text file path.

3) Check Text file is Not Empty.

4) Print your Text file lines.

Java Code :

import java.io.BufferedReader; 
import Java.io.FileReader; 
public class ReadLine
{
  BufferedReader br = new BufferedReader(new FileReader("youtFile.txt"));
  try
  {
    String line;
    while((line= br.readLine())!=null)
    {
        System.out.pringln(line)
    }
  }
 catch(Esception exo)
 {
   
 }
 finally
 {
    br.close();
  }


I hope this article useful to you. So, Please like and share.
Thank you.




Comments

Popular posts from this blog

10+ Ideas about beautiful rangoli designs | Diwali, New Year, Events

10+ Ideas about beautiful rangoli designs | 10+ Ideas about beautiful rangoli designs | Diwali, New Year, Corporate Event, Gudi Padwa, Pongal  Diwali rangoli patterns and designs started many centuries ago. Some reference of rangoli designs are also available in our scriptures and designed by prashant javia and misha javia.  Watch this wonderful rangoli patterns and designs video and download Rangoli Designs video for Diwali, Newyear, Commercial use and Competition and Decorate your home this diwali with special designs of rangoli. All Most Rangoli designs which you can make use of in all occasions. The occasions can be Diwali, New Year, Ugadi or Gudi Padwa, Pongal, Corporate Event

Smart Cities Mission

Smart Cities Mission เคนेเคฒो เคซ्เคฐेंเคก्เคธ เค†เคœ เคฎें เคฏเคนा เคชे เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी( Smart city ) เคนोเคคा เค•्เคฏा เคนे ! เค‡เคธเค•ी เคนเคฎें เคœเคฐुเคฐเคค เค•्เคฏा เคนे ! เค‡เคจเคธे เคนเคฎें เค•्เคฏा เคซाเคฏเคฆा เคนोเค—ा ! เค‡เคธ เค•े เคฌाเคฐे เคฎें เคฌเคคाเคŠंเค—ा । เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी เค•े เคฒिเค เค•्เคฏा เค•्เคฏा เคœเคฐुเคฐी เคนै।    เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी( Smart city ) เค•े เคฒिเค เค†เคงुเคจिเค• เคคเค•เคจीเค•, เคœเคจ เคธुเคตिเคงा, เคธुเคฐเค•्เคทिเคค เคชเคฐ्เคฏाเคตเคฐเคฃ, เคชเคฐिเคตเคจ เคต्เคฏเคตเคธ्เคคा, เค‰เคจ्เคจเคค เค•ाเคฐोเคฌाเคฐ เค”เคฐ เค‡ंเคŸเคฐเคจेเคถเคจ เคชเคนोเคš เคœเคฐुเคฐी เคนोเคคी เคนै।  เคเค• เคคเคฐเคน เคธे เค‡เคธเค•ो เคนเคฎ เคธिเคŸी เค•ी เคคเคธ्เคตीเคฐ เคฌเคฆเคฒเคจे เค•ी เคฏोเคœเคจा เคญी เค•เคน เคถเค•เคคे เคนै।  เคฆुเคจिเคฏा เค•े เคŸॉเคช 5 เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी ( Top 5 Smart City in World ) - Tokyo - Landon - New York - Jyurikh - Peris  เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी เคฌเคจाเคจेเค•ी เคœเคฐुเคฐเคค เค•्เคฏा เคชเฅœी।  เคœो เคนเคฎाเคฐी เคชॉเคชुเคฒेเคถเคจ เคฌเฅ เคฐเคนी เคนै เค”เคฐ เค‰เคจเค•े เค•ाเคฐเคฃ เคฐเคนเคจे เค•े เคฒिเค เคœो เคช्เคฐॉเคฌ्เคฒเคฎ เคนो เคฐเคนे เคนै เคตो เค‡เคธ เคฎिเคถเคจ เคธे เคนเคฎाเคฐी เคช्เคฐॉเคฌ्เคฒเคฎ เค•ो เค•เคฎ เค•เคฐ เคถเค•เคคे เคนै เค”เคฐ เค‡เค•ॉเคจเคฎी เคช्เคฐोเค—्เคฐेเคธ เคญी เค•เคฐ เคถเค•เคคे เคนै।  เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी เคธे เคนเคฎें เค•्เคฏा เคซाเคฏเคฆा เคนोเค—ा।   เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी เคฎें เค•เคˆ เคธाเคฐी เค‡ंเคŸเคฐเคจेเคถเคจ เค•ंเคชเคจी เคฏा เค†เคँเค—ी เคคो เค‰เคธเค•े เค•ाเคฐเคฃ เคนเคฎें เคฐोเคœเค—ाเคฐी เคฎिเคฒ เคถเค•ेंเค—ी। เคธ्เคฎाเคฐ्เคŸ เคธिเคŸी...

PM Narendra Modi Launches 'Clean India Campaign'

PM Narendra Modi Launches 'Clean India Campaign' เคธ्เคตเคš्เค› เคญाเคฐเคค เค…เคญिเคฏाเคจ( swachh bharat abhiyan ) เคเค• เคเคธा เค…เคญिเคฏाเคจ เคนै เคœिเคธเค•ा เคธंเคšाเคฒเคจ เคญाเคฐเคค เคธเคฐเค•ाเคฐ เค•เคฐเคคी เคนै । เค‡เคธ เค…เคญिเคฏाเคจ เค•ी เคถुเคฐुเค†เคค เคนเคฎाเคฐे เคช्เคฐเคงाเคจเคฎंเคค्เคฐी เคฎाเคจเคจीเคฏ เคถ्เคฐी เคจเคฐेंเคฆ्เคฐ เคฎोเคฆी เคœी เคจे 2 เค…เค•्เคŸूเคฌเคฐ, 2014  เค•ो  เคฐाเคœเค˜ाเคŸ, เคจเคˆ เคฆिเคฒ्เคฒी เคธे เค•ी । เคธ्เคตเคš्เค› เคญाเคฐเคค เค…เคญिเคฏाเคจ เคเค• เคเคธी เคชเคนเคฒ เคนै เคœो เคนเคฎाเคฐे เคฆेเคถ เค•ो เคฆुเคจिเคฏा เคฎें เคเค• เค…เคฒเค— เคชเคนเคšाเคจ เคฆेเค—ी , เคนเคฎाเคฐे เคฆेเคถ เคฎें เคธ्เคตเคš्เค›เคคा เค•ो เคฒेเค•เคฐ เคฒोเค—ों เค•ी เคœो เค—เคฒเคค เคฎाเคจเคธिเค•เคคा เคนै เค‰เคธे เค‡เคธ เคฎुเคนीเคฎ เค•े เคฆ्เคตाเคฐा เคนी เคฌเคฆเคฒा เคœा เคธเค•เคคा เคนै । เค…เคฌ เคฌเฅœा เคช्เคฐเคถ्เคจ เค†เคชเค•े เคฎเคจ เคฎें เคฏเคน เค†เคคा เคนै เค•ी เค†เค–िเคฐ เคฏे เคธ्เคตเคš्เค› เคญाเคฐเคค เค…เคญिเคฏाเคจ(Clean Pollution Free-India) เคนै เค•्เคฏा? เคธ्เคตเคš्เค› เคญाเคฐเคค( Swachh Bharat ) เค…เคญिเคฏाเคจ เคเค• เคฌเคนुเคค เคฌเฅœी เค”เคฐ เคฎเคนเคค्เคตเค•ांเค•्เคทी เคฏोเคœเคจा เคนै เคœिเคธเคฎे เคญाเคฐเคค เค•ा เคนเคฐ เค›ोเคŸे เคธे เค›ोเคŸा เค—ाँเคต, เฅ˜เคธ्เคฌा เค”เคฐ เคถเคนเคฐ เคธเคฎ्เคฎिเคฒिเคค เคนै। เคฏे เค…เคญिเคฏाเคจ เคเค• เคธोเคš เคนै เคœिเคธे เคนเคฐ เคญाเคฐเคคीเคฏ เคคเค• เคชเคนुंเคšाเคจा เค†เคตเคถ्เคฏเค• เคนै เค”เคฐ เคนเคฐ เคญाเคฐเคคीเคฏ เค•ो เคธ्เคตเคš्เค›เคคा เค•ी เค…เคนเคฎिเคฏเคค เคชเคคा เคนोเคจी เคนी เคšाเคนिเค เค•ि เคธ्เคตเคš्เค›เคคा เคจ เคธिเคฐ्เคซ เคนเคฎें เคธเคฎाเคœ เคฎें เคธเคฎ्เคฎाเคจ เคฆिเคฒाเคคी เคนै เคฌเคฒ्เค•ि เคฏे เคนเคฎें เคญเคตिเคท्เคฏ เคฎें เค•เคˆ เคฌीเคฎाเคฐिเคฏो เค”เคฐ เคธเคฎเคธ्เคฏाเค“ं เคธे เคญी เคฌเคšाเคจे เคฎें เคเค• เค…เคนเคฎ เค•िเคฐเคฆाเคฐ เคจिเคญाเคคी เคนै । เคธ्เคตเคš्เค›เคคा เค•ा เคœो เคตिเคทเคฏ เคนै เค‡...