• 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

EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My servlet will be getting a file which may be a CSV (comma separated values) or a fixed length file. how do i extract the data from those files . can any body help me please.. i require to extract these data and insert in a database.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For CSV one would normally use either a java.util.StringTokenizer if the file is broken into lines or a java.io.StreamTokenizer. These objects will break up a String or a stream of text recognizing comma or whatever you set as a separator.
Bill
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
ya for the CSV using StringTokenizer is the right approach
fro fixed length file create a
FileInputStream fis = new FileInputStream(filein);
where filein is the fixed length file
then read the contents of the file and put in the byte array or u can use File Reader also
------------------
 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnks namita and william
but now can you please tell me how to create a fixed length file.I have extracted the data from the csv files now I'll have to created a fixed length txt file whose format i know... I suppose ill have to pad some spaces etc ...please tell me how to do that
Thanx in advance
 
namita pa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
to create a file and write the data into it you can try
File file = new File(strFileName);
StringWriter stringwriter = new StringWriter();
PrintWriter printwriter = new PrintWriter(stringwriter); Date date = new Date(System.currentTimeMillis());
String s1="data1";String s2="data2";
String s3 = "\n \n***********New Exception************* Date :" + s2;
String s4 = " \n Exception from :" + s1 + "\n\n Description :" + stringwriter.toString();
String s5 = s3 + s4;
FileOutputStream fileoutputstream = new FileOutputStream(file.getPath(), true);
byte abyte0[] = s5.getBytes();
for(int i = 0; i < abyte0.length; i++)
fileoutputstream.write(abyte0[i]);
fileoutputstream.close();
/////////////////////////////////////////////
i.e you open a file o/p stream and get a byte array from the formated text s5 that you have created using your data s1,s2 etc thenwrite into this stream and close the stream on the file
hope this bit of code will help you solve your problem
all the best
any problem write back
regards
namita


------------------
 
namita pa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
create a file as
File file = new File(strFileName);
StringWriter stringwriter = new StringWriter();
PrintWriter printwriter = new PrintWriter(stringwriter);
// printwriter.println(exception2.getMessage());
String s2 = "";String s1="";
String s3 = "\n \n***********New Exception************* " + s2;
String s4 = " \n Exception from :" + s1 + "\n\n Description :" + stringwriter.toString();
String s5 = s3 + s4;
FileOutputStream fileoutputstream = new FileOutputStream(file.getPath(), true);
byte abyte0[] = s5.getBytes();
for(int i = 0; i < abyte0.length; i++)
fileoutputstream.write(abyte0[i]);
fileoutputstream.close();
////////////////////
you create a fileo/p stream on the file
and then a byte array on the formated text you have written as s5 in this case using your extracted data s1,s2 etc
then write into this file as shown and close the stream .
hope this would help you in solving your problem.
all the best
write back if some problem is there
regards
namita
------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic