Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reading csv file content into a string

    Hi Experts,
    I need to read the attached csv file into a string. And I need the commas(,) to be replaced by exactly \t and new lines by exactly \n.
    The final string should be in the form
    "val1\tval2\tval3\nval4\tval5\tval6\n"

    I have tried the below code for reading the file first but it repeats the last few lines at the end.donno why..
    the code i tried is:

    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(
    new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
    fileData.append(buf, 0, numRead);
    }
    reader.close();
    return fileData.toString();



    Can you please give me a java code which reads gthe file without any repetition and replace commas with \t and new lines with \n

    Your help will be highly appreciated.
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    The code you posted seems to be okey...
    Once the CSV file is read into string. you can use string function
    replaceAll() to replace comma with \t and new line with \n

    See http://java.sun.com/j2se/1.4.2/docs/...ng/String.html for replaceAll()


  •  

    Posting Permissions

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