aspose file tools*
The moose likes Java in General and the fly likes reading an CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "reading an CSV file" Watch "reading an CSV file" New topic
Author

reading an CSV file

Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
hi
just want to read an CSV file which is delimited by commas
example file contents
1,2,3,4,5
a,b,c,d,e

i should be able to understand if it is a end of line , or if it is a end of file...

can anybody guide me with a piece of code or URL to accomplish this
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I suggest you check out FileReader, BufferedReader and the split() method of String


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
but StringTokenizer..wilnot be a better option..
I am just trying to see if i can get some exampel for doing this kind of manipulation
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Amar Naik:
but StringTokenizer..wilnot be a better option..


NO. People generally shouldn't use StringTokenizer for anything anymore; use String.split() instead. It's more flexible and more efficient.


[Jess in Action][AskingGoodQuestions]
Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
i apologize if i am asking some silly questions..just trying to get things as early as possible with the help of forum

var s = "red, blue, orange, bright yellow , light gray, black";

var ary = s.split(RegExp(" *, *", "gi"));

for (var i = 0; i < ary.length; i++)
Response.write(ary[i]);

but how will i know in my file that an end of line or end of file is reached using string SPLIT..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Are we talking about Java, or JavaScript? Because that sure looks like JavaScript. Note that the forum you're in is for discussing Java, not JavaScript. They're totally different languages.

You do the same thing with String.split() that you'd do with StringTokenizer: you read in one line at a time with BufferedReader.readLine(), and then call split() on just that one line. When readLine() returns null, you're at EOF.
Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
yes , readline NULL indicates that it is an end of file..but my issue is
1. each row of data as multiple colums delimited by ','..so how can i tell the program that after each ',' we will get a new row
2. how do i know that i reached an end of line...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Unless this is a learning exercise, why not use an existing library fro reading and writing CSV? CSV has a number of special cases to consider, and before you have rolled your own that handles these, you might as well use something existing.


Ping & DNS - my free Android networking tools app
Amar Naik
Ranch Hand

Joined: Sep 26, 2003
Posts: 43
mine example content in the file is
Line 1 : ,1,2,3,4,5,6,7
Line 2 : ,a,b,c,d,e,f,g

Output i want to display as
1. 1 2 3 4 5 6 7
2. a b c d e f g

here is my piece of code
import java.io.*;

import java.util.*;

public class ReadCSVFile
{
public static void main(String[] args)
{

try
{
BufferedReader in = new BufferedReader(new FileReader("RecordsNet DocType List1.csv"));
String str;
int i =0;
while ((str = in.readLine()) != null)
{

StringTokenizer parser = new StringTokenizer(str,",");
while (parser.hasMoreTokens())
{
String doctype=parser.nextToken();
String doctypeid=parser.nextToken();
String docname=parser.nextToken();
String site=parser.nextToken();
String Freq=parser.nextToken();
String DOCCATEGORY_NM =parser.nextToken();
String CREATION_DATE =parser.nextToken();
String CLR_JOBS =parser.nextToken();
String Version = parser.nextToken();
i=i+1;
}

}

in.close();
}
catch (IOException e)
{
}

}//class main end
}// class ReadCSVFile end


i am getting the error as
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:259)
at ReadCSVFile.main(ReadCSVFile.java:29)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading an CSV file