Home Jsp Retrieve Data from CSV file in JSP



Retrieve Data from CSV file in JSP
Posted on: September 26, 2008 at 12:00 AM
CSV file : A CSV file is commonly known as a Comma Delimited File or a Character Separated File.

Retrieve Data from CSV file in JSP

     

CSV file : A CSV file is commonly known as a Comma Delimited File or a Character Separated File. It is a text file that has a specific format which allows saving of text in organized manner. This format known as a flat table, is very simple. Each row contains one record of information. The character used to distinguish each piece of data within each record is most commonly used a comma ",".

"myfile.csv" in the c:\csv folder


Create a Page ("ReadCsvFile.jsp") to retrieve the data from CSV file "
myfile.csv"

<%@ page import="java.io.*"%>
<html>
<body>
<% 
   String fName = "c:\\csv\\myfile.csv";
   String thisLine; 
  int count=0; 
  FileInputStream fis = new FileInputStream(fName);
  DataInputStream myInput = new DataInputStream(fis);
  int i=0; 
%>
<table>
<%
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
if(i!=0)
{
out.print(" " +strar[j]+ " ");
}
else
{
out.print(" <b>" +strar[j]+ "</b> ");
}
}
out.println("<br>");
i++;

%>
</table>
</body>
</html>


Output


Download full source code

 

Related Tags for Retrieve Data from CSV file in JSP:
cguicomfileormtabletextdatauiformcsviosedformatcharvilimitthisaicharactersimpletabrowiftexforierecordctewithtocontainseachaviinformationciwsshexteilitcommalimitusepeimmanceinnormsavinginfocsasmntparseparatethinstiososspecflatcommonallpiepiratewhichsspdelimiteddelatrackishainfllmplflaeaannaractspecificsasimxtvinzssrdthavstabablatihatgainforminformatorgpiecepleplmostmionlyonomonl


More Tutorials from this section

Ask Questions?    Discuss: Retrieve Data from CSV file in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.