• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

reading an CSV file

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you check out FileReader, BufferedReader and the split() method of String
 
Amar Naik
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Amar Naik
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amar Naik
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!