Results 1 to 12 of 12

Thread: [RESOLVED] Java CSVFileReader

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Resolved [RESOLVED] Java CSVFileReader

    How do I import the java CSVFileReader and how do I use it?

  2. #2
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    Well what kind of things you want to do with the CSV file?

    You simply need to add the relevant header file. Depending on the method chosen.

    http://www.exampledepot.com/egs/java...ParseLine.html - this mite be of help

    If you simply want to open the actual CSV file then 'import java.io.*;' should do the trick with the following code:

    Code:
    Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " +
                        "strFileLocation");
    where strFileLocation = the location of the file you want to open

    Regards,
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Java CSVFileReader

    the problem is when Im tokenizing the line with commas, im missing cells with commas in them. The way csv files are created is if there is a comma in the cell then quotes are put around the token. I read that there is a built in way of taking care of this common probelm in php and im sure something exists with java, i just would like to know what it is and how to use it.

  4. #4
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    Here you could also try this out:

    Code:
    String [][] strNum = new String [30][30];
     
    File f = new File("filename.csv");
     
    BufferedReader br  = new BufferedReader(new FileReader(f));
    String strLine = null;
    int row = 0;
    int col = 0;
     
    //read each line of text file
    while((strLine = br.readLine()) != null && row < 30)
    {	
    	StringTokenizer st = new StringTokenizer(strLine,",");
    	while (st.hasMoreTokens())
    	{
    		//get next token and store it in the array
    		strNum[row][col] = st.nextToken();
    		col++;
    	}
    	col = 0;
    	row++;
    }
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  5. #5
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    not sure whether the above post will help you though (didnt know you had commas in your CSV file)...what kind of data is stored in your CSV?
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java CSVFileReader

    Quote Originally Posted by Greyskull
    not sure whether the above post will help you though (didnt know you had commas in your CSV file)...what kind of data is stored in your CSV?
    CSV = Comma Separated Values.
    This means any kind of values
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    Quote Originally Posted by ComputerJy
    CSV = Comma Separated Values.
    This means any kind of values
    Sorry i think i said that wrong . i knw what CSV file is but what i was trying to say was im not sure how to do it if one of his values contained a comma as i knw that the application will think its the end of line...
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java CSVFileReader

    when using CSV, all commas in original data must be encoded to something like a tag so it can be decoded when reading the file without causing any problems
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Java CSVFileReader

    So do you guys know where I can get a class that works?

  10. #10
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    The following link maybe of help

    http://faq.javaranch.com/java/AccessingFileFormats

    Under the Excel files, there are three links you could try with CSV files.

    tbh, i havent really tried inserting commas to any of the fields stored in a CSV file as i knew i will encounter problems such as this, instead, if i had to store i value containing commas, i will use a database.

    Regards,
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  11. #11

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Java CSVFileReader

    Cool, that looks pretty solid. I used this one, it works great

    http://opencsv.sourceforge.net/

  12. #12
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Java CSVFileReader

    Glad to hear it Don't forget to resolve the thread
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

Posting Permissions

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



Click Here to Expand Forum to Full Width