Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By Norm

Thread: Writing to existing CSV file

  1. #1
    tuxman is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Writing to existing CSV file

    Hello !

    I have many CSV files like this:

    Dist H G
    12.5 6.67 8.76
    3.54 8.32 7.43


    I want to make a simple application in Java for:
    -choose a CSV file(with File Chooser Dialog)
    -create a new column("Sum") and put the result of ("H" + "G") there

    The result CSV:
    Dist H G Sum
    12.5 6.67 8.76 15.43
    3.54 8.32 7.43 15.75

    Please help me to write the code.

    Thank you very much in advance

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Writing to existing CSV file

    Break the task up into a list of simple steps and do one step at a time
    Start with a simple program that reads the file and parses the columns on each line into an array.
    Then add the extra column to the array
    Then write the contents of the array to a disk file.
    quad64bit and Fubarable like this.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Writing to existing CSV file

    Yeah, Norm outlined a good process. Are you having trouble with a specific portion of your code?

  4. #4
    tuxman is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Writing to existing CSV file

    Till now, my code is:

    public static void main(String[] args) {

    try {


    Scanner myfile = new Scanner(new FileReader("test1.csv"));

    while (myfile.hasNextLine()) {
    String[] cols = myfile.nextLine().split(";", 2);
    System.out.println("col1: " + cols[0] + ", col2:" + cols[1]);
    }

    myfile.close();



    } catch (Exception e) {
    e.printStackTrace();
    }

    }


    Then add the extra column to the array
    How ?

    Thank very much

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Writing to existing CSV file

    See the Arrays class for utility method to copy old array to new array.

  6. #6
    tuxman is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Writing to existing CSV file

    How do I add new column to my array ?

    With System.arraycopy ?!

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Writing to existing CSV file

    Did you try the Arrays class method? What happened?

  8. #8
    tuxman is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Writing to existing CSV file

    Hello Sir !
    I don't know how to add a new column.
    So, please help me.

    Thank you very much in advance

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: Writing to existing CSV file

    The Arrays class method will copy data from one array into a new array and add extend the size of the new array.
    For example if you have a String[4] it will copy the 4 elements from that array into a String[5] array, adding an extra element to the end of the array.

Similar Threads

  1. Replies: 0
    Last Post: 12-21-2011, 10:15 AM
  2. how to put existing file in ftp
    By trkece in forum Networking
    Replies: 0
    Last Post: 02-11-2011, 06:42 AM
  3. How tor write into Existing File
    By agajantorayev in forum New To Java
    Replies: 5
    Last Post: 08-17-2010, 02:03 PM
  4. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  5. Writing to excel file erasing existing formatting
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 06:58 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
  •