This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Required to read a file content

 
Nag raja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one txt file which has content like below and the content is separated by '|'.

3034661356|001|788-33-4958
4035661356|002|788-33-4958
5034661356|003|788-33-4958
6034661356|004|788-33-4958
7034661356|005|788-33-4958

I need to capture the first and second fields of the file in all lines.for example

3034661356|001|788-33-4958

In the above line i need only 3034661356 and 001 but not 788-33-4958.
How can read the first two fields of each line in a file.
If you provide code it will be very helpful for me.
thanks
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you need a BufferedReader to read each line of the file.
Then, for each line, you can call the split method to get you string represented as an array of String without the '|' character.

This code may help you doing this :

 
Nag raja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its only printing first two numbers. For instance in the following data

3034661356|001|788-33-4958
4035661356|002|788-44-4958
5034661356|003|788-55-4958
6034661356|004|788-66-4958
7034661356|005|788-77-4958

your code output is

30

40

50

60

70

The first two numbers of each line.
Please help me
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evan Caballero wrote:

Almost. | is a meta character in regular expressions meaning the left hand side or the right hand side. In this case it is "nothing or nothing". You need to escape it:
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nag raja wrote:Thank you very. I need the exact code

We are not a code mill. We will help you find your own answers, but in the end you must do the hard work/
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Rob, my code wasn't tested, it was just for helping.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, I was just adding my comment because regular expressions are a difficult subject, and the mistake of using simply "|" has been made before by many people.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!