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.
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
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 ------------------
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
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
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 ------------------
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking