Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: searching a string in a file

  1. #1
    sathya11491 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default searching a string in a file

    hello guys

    i have a text file of size 10mb. i need to search for a string and get its position in the file.

    also is it possible to search a file from a given position to end-of-file?

    please help me out.........

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: searching a string in a file

    What have you tried so far?
    Read the file line by line
    Search each line as it is read.
    The RandomAccessFile class will allow you to read at any byte location in the file. If the file has lines there is no way to know where lines begin unless the size of all the lines is known.
    A line is a String that ends with a lineend character. ("\n")
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: searching a string in a file

    Haven't-a-clue questions don't belong in Advanced Java. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    sathya11491 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: searching a string in a file

    norm:

    the file under consideration is a csv file. there are 12 fields seperated by comma in each line.
    consider this situation:

    An outside application keeps on appending "set of records" for every 5 sec in the csv file.
    I need to insert these newly appended records in my database
    these are the constraints:
    1) there is no primary key i.e no record has a unique field/collection of fields
    2) the no of records inserted in the csv file per 5 sec may vary

    so how do i approach this situation? what is the best way to keep track of last record inserted in the database from the text file?or how do i know from which record i need to insert into the database after 5 sec?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: searching a string in a file

    That sounds a rather fragile thing.
    I hope this is simply papering over an already existing old system...:)

    Anyway, you'll need to maintain a line number somewhere so you know which ones have already been inserted, then simply read through the file until you hit that line number (there's a LineNumberReader class, or something like that).
    This will, presumably, need to be persisted, so it will have to be in the db for safe keeping.
    sathya11491 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Searching for tags in a string
    By Warcrea in forum New To Java
    Replies: 1
    Last Post: 02-16-2012, 12:44 AM
  2. Searching for a string in different log files
    By nitin kishore in forum Advanced Java
    Replies: 6
    Last Post: 09-14-2011, 10:32 PM
  3. searching for a word in a string
    By Brian-82 in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 08:19 PM
  4. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 10:50 AM
  5. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •