Home Jsp Retrieve Data from CSV file in JSP

Related Tutorials


 
 

Share on Google+Share on Google+

Retrieve Data from CSV file in JSP

Advertisement
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

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 26, 2008

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Blue
April 25, 2012
import jspBeans to csv

Hello, I would like to ask that, I have a jsp page and I get retrieve the data to jsp table by using jspBeans.Now I want to import this data to csv. Could you help me please. this is my jsp page: <c:forEach var="amlist" begin="0" end="${aml.amlist.size()-1}" step="1"> ${'data.addRows( [[\'rev\','} ${aml.amlist.get(amlist).sfactoryMethod}${',' } ${aml.amlist.get(amlist).sprototype}${',' } ${aml.amlist.get(amlist).ssingleton}${',' } ${aml.amlist.get(amlist).sadapter}${',' } ${aml.amlist.get(amlist).scomposite}${',' } ${aml.amlist.get(amlist).sdecorator}${',' } ${aml.amlist.get(amlist).sobserver}${',' } ${aml.amlist.get(amlist).sstateStrategy}${',' } ${aml.amlist.get(amlist).stemplateMethod}${',' } ${aml.amlist.get(amlist).svisitor}${',' } ${aml.amlist.get(amlist).sproxy}${',' } ${aml.amlist.get(amlist).sproxy2}${']]);'} </c:forEach>
DMCA.com