• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading comma seperated text file

 
Rasty Binfoh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rasty Binfoh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston and Chris,
Thanks for the recommendations. Let me try and get back to you
 
Rasty Binfoh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably too difficult for “beginning”. Moving discussion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic