Results 1 to 13 of 13
  1. #1
    10x2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default 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
    Attached Files Attached Files

  2. #2
    10x2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    need help please

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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;
    	}
    The method returns true if it managed to convert an entire String line, it returns false otherwise; and here's a method that exercises the above method a bit:

    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);
    	}
    I leave it up to you to write the converted String (stored in that list) to your output file.

    kind regards,

    Jos
    Last edited by JosAH; 04-30-2010 at 09:45 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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();
    		}
    
    	}
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Why do that?
    Why simply code dump?
    How is someone supposed to actually learn how to do things if you simply code dump?

  7. #7
    10x2 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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...

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 10x2 View Post
    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
    So you went for the boilerplate code, ignoring the other replies? How typical ...

    kind regards,

    Jos

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by 10x2 View Post
    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

    then insert the line before out.write ....

    Java Code:
    line = line.replaceAll("\"", "");

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Darn, this has been crossposted as well ...

    kind regards,

    Jos

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    Because they used a StringTokenizer (which is deprecated)

    in the java 6 api i don't see any hints about StringTokenizer ist deprecated. can you please send me a link.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    in the java 6 api i don't see any hints about StringTokenizer ist deprecated. can you please send me a link.
    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.
    kind regards,

    Jos

Similar Threads

  1. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  2. Import / Export Issue
    By Gideonzx in forum Eclipse
    Replies: 0
    Last Post: 07-23-2009, 08:08 PM
  3. Import files from NetBeans
    By Juuno in forum Eclipse
    Replies: 2
    Last Post: 04-08-2009, 05:20 AM
  4. unable to import jar files (crystal report 11)
    By kishore101 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 07:51 AM
  5. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •