Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to append line in csv file  RSS feed

 
mayur bhishma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I have a csv file and I want to insert the csv file into the database columns consist of date,description1,description2,amount1,amount2,narration..
but the problem is the narration is coming in second line in csv file due to which the data is not getting inserted into the database.
so how to append the narration into the first line,I have enclosed the demo format of csv file and a code to read the csv file..

myFile.csv file:


code to read the csv file:

<%@ 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>



 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, I/O should never be done in a JSP. Move that code to a servlet or backing bean.

Secondly, CSV is not as simple as you assume it is. It is perfectly OK for a CSV row to be split over multiple lines. You also seem to disregard the double quotes that can (and do) surround cells. I strongly advise to use one of many CSV libraries instead of trying to roll your own; http://www.coderanch.com/how-to/java/AccessingFileFormats lists several.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!