Results 1 to 13 of 13
- 04-29-2010, 09:05 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
import from dsv files and export to csv files
hi ..
i am just started learning java from two months
and i need to create a project that does the following
1- import a .dsv (Delimiter-Separated Values) file content and save it to array
- the values in this format separated by fixed commas
example
"AIG" "Insurance" "64.91" "25/11/06"
2- export into .csv file (Comma Separated Value)
-the values in this format separated by commas
example:
AIG,Insurance,64.91,25/11/06
so if any one can help by explaining how to import and export these files
p.s there are example files in the attachment
- 04-30-2010, 09:11 AM #2
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
need help please
- 04-30-2010, 09:40 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Suppose s is a line from your input file; finding the first double quote is easy, the indexOf( ... ) method can do it; if such a quote is found it is the opening quote and finding the first closing quote is easy too, the same indexOf( ... ) method can do it. Let those positions be i and j, such that j > i. You can store the substring(i+1, j) in a list that collects them all; note the i+1 because we don't want the opening quote to be part of the data substring. Repeat this scenario until you can't find a next i anymore. Here's a method that does it:
Java Code:private static boolean process(String s, List<String> list) { for (int j, i= -1; (i= s.indexOf('\"', i+1)) >= 0; i= j) { j= s.indexOf('\"', i+1); if (j < 0) return false; list.add(s.substring(i+1, j)); } return true; }
Java Code:public static void main(String[] args) { String s= "\"foo\" bar \"baz\" bar "; List<String> list= new ArrayList<String>(); if (process(s, list)) System.out.println(list); }
kind regards,
JosLast edited by JosAH; 04-30-2010 at 09:45 AM.
- 04-30-2010, 10:14 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
I'd (and I have done) split on "\" \"", and remove the leading quote from the first String returned, and the trailing quote from the last String. That gives you your array of values that you then write out to your csv.
Do this a line at a time. read a line from the dsv, split it, then write to the csv.
- 04-30-2010, 11:03 AM #5
this peace of code works with your inputfiles.
Java Code:import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.StringTokenizer; public class MyTokenizer { public static void main(String[] args) { BufferedReader in; BufferedWriter out; File fileIn = new File( // specify here your inputfile "Drive:/yourPathForInput/DSV_example.txt"); File fileOut = new File( // specify here your outputfile "Drive:/yourPathForOutput/DSV_test.txt"); try { in = new BufferedReader(new FileReader(fileIn)); out = new BufferedWriter(new FileWriter(fileOut)); String nextLine = ""; String line = ""; while ((nextLine = in.readLine()) != null) { // not optimal to create a new StringTokenizer StringTokenizer st = new StringTokenizer(nextLine); while (st.hasMoreTokens()) { // if needed, change your delimiter here line += st.nextToken() + ","; } // the last delimiter in the line must be deleted System.out.println(line.substring(0, line.length() - 1)); // "\r\n" are hard-coded, better to use the line.separator out.write(line.substring(0, line.length() - 1) + "\r\n"); line = ""; } in.close(); out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
- 04-30-2010, 11:12 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Why do that?
Why simply code dump?
How is someone supposed to actually learn how to do things if you simply code dump?
- 04-30-2010, 11:27 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
thanx j2me64
it worked .. but what i have to change to remove " the quotes from the output files i need it to be separated by commas "," only
- 04-30-2010, 11:32 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Because they used a StringTokenizer (which is deprecated) it is splitting on a space.
Do as I suggested and use split().
And people wonder why I gripe about code dumps...
- 04-30-2010, 11:35 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
- 04-30-2010, 11:43 AM #10
- 04-30-2010, 11:48 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Darn, this has been crossposted as well ...
kind regards,
Jos
- 04-30-2010, 11:49 AM #12
- 04-30-2010, 11:56 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
In my API docs (version 1.6) it reads:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Jos
Similar Threads
-
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
Import / Export Issue
By Gideonzx in forum EclipseReplies: 0Last Post: 07-23-2009, 08:08 PM -
Import files from NetBeans
By Juuno in forum EclipseReplies: 2Last Post: 04-08-2009, 05:20 AM -
unable to import jar files (crystal report 11)
By kishore101 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-25-2009, 07:51 AM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM
Bookmarks