GeeCON Prague 2014*
The moose likes Servlets and the fly likes EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE" Watch "EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE" New topic
Author

EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE

mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
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
namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56
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

Joined: Sep 12, 2001
Posts: 81
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

Joined: Sep 06, 2001
Posts: 56
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

Joined: Sep 06, 2001
Posts: 56
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
------------------
 
GeeCON Prague 2014
 
subject: EXTRACTING DATA FROM CSV OR FIXED LENGTH FILE