• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading multiple CSV files

 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about something like:



Joe
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic