how to read particular data from file in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdulk
    New Member
    • Jan 2014
    • 1

    how to read particular data from file in java

    i am very new to java could you please help me out my problem.
    I need to write program in java to read line where code = "58".

    Date time code value

    04-21-1991 9:09 58 100
    04-21-1991 9:09 33 009
    04-21-1991 9:09 34 013
    04-21-1991 17:08 62 119
    04-21-1991 17:08 33 007
    04-21-1991 22:51 48 123
    04-22-1991 7:35 58 216
    04-22-1991 7:35 33 010
    04-22-1991 7:35 34 013
    04-22-1991 13:40 33 002
    04-22-1991 16:56 62 211
    04-22-1991 16:56 33 007
    04-23-1991 7:25 58 257
    04-23-1991 7:25 33 011
    04-23-1991 7:25 34 013
    04-23-1991 17:25 62 129
    04-23-1991 17:25 33 007
    04-24-1991 7:52 58 239
    04-24-1991 7:52 33 010
    04-24-1991 7:52 34 014
    04-24-1991 12:00 33 004
    04-24-1991 17:10 62 129
    04-24-1991 22:09 48 340
    04-24-1991 22:09 33 005
    04-25-1991 7:29 58 067

    This is how my text file looks like.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hello abdulk and welcome to bytes.com!

    The easiest way to do this is probably to read the file line by line and checking for each one whether the code equals 58. For reading and writing files check this article, for more information about reading input with the Scanner class (which I would recommend) there's this one.

    Once you can look at the lines one by one, you'll have to check them for the code. There are a few ways to do this, the most elegant is probably using a regular expression (or "regex" for short). If having an elegant solution is not your main concern (e.g. because you are very new to Java) you could also do something else, e.g. check whether the string " 58 " is contained in the line.

    Comment

    Working...