Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
|
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2006, 11:04 AM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default parse a csv file

Hi there,

I am trying to parse a CSV file, I have a program which will read in the contents of the file and display them to the console and i need to parse some of the fields, which I have no idea how to do
I am trying to get the field info into to an SWT table. The csv file I'm trying to parse has some useless information before and after the file info which is numbered [0-n].
I need to get each relevent line and take its individual pieces of data and store them in an array or something so I can put them into my table i.e. '0:' in one tableItem and 'bla.jpg' in the next tableItem.

Any help or code samples would be much appreciated,

Thanks in advance,

Shinny

--START OF CSV FILE--
File: penDriveImage3.001
Start: Wed Aug 02 19:49:44 2006
Length: 992 MB (1040187392 bytes)

Num Name (bs=512) Size File Offset Comment

0: 8426.jpg 5 KB 4314525 (null) /*THESE LINES ARE IN ONE FIELD EACH!?!*
1: 13578.jpg 42 KB 6952364 (null)
2: 13663.jpg 27 KB 6995952 (null)
3: 13791.jpg 26 KB 7061467 (null)
4: 14825.jpg 46 KB 7590486 (null)
5: 16830.jpg 83 KB 8617069 (null)
6: 16999.jpg 36 KB 8703995 (null)
...RANDOM DATA HERE...
--END OF CSV FILE--

Code:
package GUI;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadSource
{
public static void main(String[] arguments)
{
try
{
FileReader file = new FileReader("C:\\audit.csv");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while(!eof){
String line = buff.readLine();
if(line == null)
eof = true;
else
System.out.println(line);
}
buff.close();
}
catch(IOException e)
{
System.out.println("Error -- " + e.toString());
}
}

}
 
Old August 16th, 2006, 03:33 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you looked at String.split(",")?

Jon Emerson
http://www.jonemerson.net/
 
Old August 29th, 2006, 02:00 PM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes u have to use the split method , i have done CSV parsing before . One thing u have to be careful about is that you should eliminate any "," that might be the part of the data in the CSV file otherwise you can have undesired results with data of column n being used as the data of the column n+1 .

 
Old September 8th, 2006, 08:14 AM
Authorized User
 
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can use StringTokenizer to separate individual fields in csv files and read them in or you may use open-source CSV parser classes available at sourceforge.


http://www.ravishakya.com
 
Old September 11th, 2006, 06:26 AM
Authorized User
 
Join Date: Jul 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have written a CSV-handler class before, and this is qhat I did.

The standard is that any data items that contain a comma are encapsulated in double-quotes.

I replaced any encapsulated commas with the code #44;, and optionally stripped the double-quotes.

You can then split the string based on commas. You can then optionally convert the occurences of #44; within the data back to commas if you wish.

so:
    public static String rationaliseCommas(String sCSVLine, boolean bRetainQuotes)
    {
        // split the string based on quotation marks
        String[] sBroken = sCSVLine.split("\"");
        String sResult="";

        // every OTHER entry was between quotes
        for (int n = 1 ; n < sBroken.length ; n=n+2)
        {
            //encode the comma that was within quotes
            sBroken[n] = sBroken[n].replaceAll(",","#44;");

            // put the quotes back if required
            if (bRetainQuotes)
            {
                sBroken[n] = "\"" + sBroken[n] + "\"";
            }
        }
        for (int n=0 ; n<sBroken.length ; n++)
        {
            sResult+=sBroken[n];
        }
        return sResult;
    }

For large CSV lines you should really use StringBuffers instead of Strings, but you get the idea






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to parse the XML file and DTD through Xerces tufailfifa C++ Programming 0 June 25th, 2007 07:03 AM
how to parse the XML file and DTD tufailfifa XML 0 June 25th, 2007 07:02 AM
data parse from text file mameworld Java Basics 1 January 14th, 2006 06:30 AM
Parse Txt File with Reg Expression harpua Classic ASP Basics 2 June 9th, 2004 12:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.