wood burning stoves 2.0*
The moose likes Java in General and the fly likes Reading comma seperated text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading comma seperated text file" Watch "Reading comma seperated text file" New topic
Author

Reading comma seperated text file

Rasty Binfoh
Greenhorn

Joined: Jul 29, 2012
Posts: 3
Hi,
I need to read the contents of a text file which contains comma separated values into my code. Code iterates until last line in text file is read, then ends. Now i am able to read a line with "strLine". See my code below so far:

TEXT FILE CONTENT (test.txt)
00000000, 05007
00000001, 05487
00000002, 05456


How do i separate strLine so i can read and iterate second field 05007, 05487 in kk.

Thanks
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1659
    
  14

You can split a string using the split() method on a String object - you need to tell the method which character to use as a delimiter e.g. a comma in your case.

See http://beginwithjava.blogspot.co.uk/2011/05/java-csv-file-reader.html for a simple example.

No more Blub for me, thank you, Vicar.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7695
    
  19

Rasty Binfoh wrote:See my code below so far:
...
How do i separate strLine so i can read and iterate second field 05007, 05487 in kk.

Hi Rasty, and welcome to JavaRanch.

First, please UseCodeTags (←click) when you're posting code. I've done it for you this time, but for future reference. And please read the page thoroughly; there are a few other little gotchas to know about as well. Thanks.

As to your question: Look at String.split(). In your case, I think you'll want:
strLine.split(",\\s*")

However, if this is a true CSV file - ie, it can contain embedded commas inside quoted strings - you'll need something more sophisticated. There are several CSV parsers out there in InternetLand.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rasty Binfoh
Greenhorn

Joined: Jul 29, 2012
Posts: 3
Hi Winston and Chris,
Thanks for the recommendations. Let me try and get back to you
Rasty Binfoh
Greenhorn

Joined: Jul 29, 2012
Posts: 3
Using a .txt file with inputs COLUMN1,COLUMN2, what will i then need to put in my column fields beow so column1 and 2 will be read seperately at each line or iteration?


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7695
    
  19

Rasty Binfoh wrote:Using a .txt file with inputs COLUMN1,COLUMN2, what will i then need to put in my column fields beow so column1 and 2 will be read seperately at each line or iteration?

You need to look at the documentation. String.split() returns something, but you're not using it.

And BTW, what you posted won't compile.

Winston
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Winston Gutkowski wrote:However, if this is a true CSV file - ie, it can contain embedded commas inside quoted strings - you'll need something more sophisticated. There are several CSV parsers out there in InternetLand.

Some of which you can find on our AccessingFileFormats page, under Excel.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
Probably too difficult for “beginning”. Moving discussion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading comma seperated text file