Results 1 to 4 of 4
  1. #1
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Question How to edit/delete a record in a csv table with JavaCsv?

    Hello,

    I am using JavaCsv: http://sourceforge.net/projects/javacsv/

    I looked through all the methods listed at: CsvWriter (JavaCSV API)

    but I could not see a way to edit delete an existing record in a CSV file. Does anyone know how to do this?

    Say I had a table:
    -------
    user_id,name,comment
    1,fogus,me
    2,mogus,myself
    3,dogus,i
    -------
    and I wanted to delete record with user_id 3.

    I could get this record by using a CsvReader object and using reader.get("user_id") looping through the file until I hit it.

    I can write new records by using a CsvWriter object and using:
    public void write(String content, boolean preserveSpaces)

    But how do I edit or delete an existing record? This is the question.

    Could someone please provide an example of how I could get that first table to (note changes in records 2 and 3)?:
    -------
    user_id,name,comment
    1,fogus,me
    2,bogus,myself
    3,dogus,the other me
    -------

    Thanks very much,
    ~fogus

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    did you try the replace method and passing in an empty string? haven't played with this before, so i don't know how it behaves.

  3. #3
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default No idea how to use replace(...)

    OK, I see the replace method now. I was blind.

    I really don't get how it works though. I cannot find any documentation. Does this replace an entire record? I need a way to either:
    1. Replace an entire record with a new record AND a way to delete a record
    2. A way to delete a record. (I can then just append a new record with write(...) I believe.)

    I have no idea of what to put in for the "(String original, String pattern, String replace)". What goes in those variables? Is this for an entire record in a file or just an attribute?

    Here is the code from the CsvWriter.java file:

    Java Code:
    	public static String replace(String original, String pattern, String replace) {
    		final int len = pattern.length();
    		int found = original.indexOf(pattern);
    
    		if (found > -1) {
    			StringBuffer sb = new StringBuffer();
    			int start = 0;
    
    			while (found != -1) {
    				sb.append(original.substring(start, found));
    				sb.append(replace);
    				start = found + len;
    				found = original.indexOf(pattern, start);
    			}
    
    			sb.append(original.substring(start));
    
    			return sb.toString();
    		} else {
    			return original;
    		}
    	}
    ~fogus

  4. #4
    fogus is offline Member
    Join Date
    Mar 2008
    Posts
    43
    Rep Power
    0

    Default

    As far as I can tell, that replace method has nothing do to do with records in the CSV file. It has return value String. I figure it is just an internal String method used to fiddle with the structure of a string.
    ~fogus

Similar Threads

  1. how to delete record from db using jsp
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-31-2009, 07:36 PM
  2. Replies: 1
    Last Post: 11-10-2008, 09:49 AM
  3. Replies: 0
    Last Post: 07-31-2008, 10:44 AM
  4. Replies: 3
    Last Post: 12-26-2007, 06:42 AM
  5. Doubt in edit and delete methods
    By ai_2007 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2007, 11:40 AM

Posting Permissions

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