• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to append line in csv file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>



 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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; https://coderanch.com/how-to/java/AccessingFileFormats lists several.
 
reply
    Bookmark Topic Watch Topic
  • New Topic