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