aspose file tools*
The moose likes Java in General and the fly likes Reading multiple CSV files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading multiple CSV files" Watch "Reading multiple CSV files" New topic
Author

Reading multiple CSV files

Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
Hello,

I have data in a CSV (Comma separated value) file and I read that text file from the client in Java. The purpose behind this is to create a mock dataset for the client. Is it possible to read multiple CSV files and update the client with different data sets? Right now I use buffered reader and file reader to read the file and String Tokenizer to read the individual tokens within the file. Can anyone tell if the above scenario is possible? Please.

Thanks,
Philip Grove
Ranch Hand

Joined: Aug 18, 2009
Posts: 68

Reading multiple files regardless of format is perfectly doable, you just have to make sure not to overwrite "old" data when reading new files.

Using StringTokenizer is strongly discouraged, it's a legacy class retained for compatability reasons. Look into the split() methods of String or the java.util.regex package. While regular expressions might be difficult at first, and more so in Java as they don't use the standard POSIX syntax, it's a very powerful tool.

If you want a object doing the splitting, then look at the Scanner class of the java.util package, it does support having a String as a source.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

I've been using a package called opencsv (http://opencsv.sourceforge.net/).

The different tokenizers are fine if the data is simple but if you're dealing with spreadsheets or databases it can get complicated.

Things like quoted or escaped commas are not that easy to deal with:
"Smith,John", senior
Tom\, Dick\, and Harry, idiom

As far as the multiple files, like Phillip I don't see the issue. What are concerned about?

Joe

It's not what your program can do, it's what your users do with the program.
Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
Philip Grove wrote:Reading multiple files regardless of format is perfectly doable, you just have to make sure not to overwrite "old" data when reading new files.

Using StringTokenizer is strongly discouraged, it's a legacy class retained for compatability reasons. Look into the split() methods of String or the java.util.regex package. While regular expressions might be difficult at first, and more so in Java as they don't use the standard POSIX syntax, it's a very powerful tool.

If you want a object doing the splitting, then look at the Scanner class of the java.util package, it does support having a String as a source.


Hello,

Thanks for the reply. I have the following code to read the CSV file. Can you explain what you are saying with some code sample? Do you want loop through all files?
My delimiter in this case is space " ".

String strFile = "C:/Users/Data.csv";

BufferedReader br = new BufferedReader(new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0;
int numbers[][] = new int[120][2];

//pretend you're looping through lines in a file here
while((strLine = br.readLine()) != null)
{
lineNumber++;

//break space separated line using " "
st = new StringTokenizer(strLine, " ");

while(st.hasMoreTokens())
{
//display echo values
int data1 = Integer.parseInt(st.nextToken());
numbers[lineNumber][0] = data1;

int data2 = Integer.parseInt(st.nextToken());
numbers[lineNumber][1] = data2;

System.out.println("Data1=" + data1 + " Data2=" + data2);

}

}
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

How about something like:



Joe
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
I generally advise to use one of the numerous available CSV libraries instead of rolling your own (see the AccessingFileFormats page for a list of such libraries). CSV has features that will break the code you posted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading multiple CSV files