Results 1 to 4 of 4
  1. #1
    sledjama is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default I need to truncate a file with 10 or more lines, to 10, removing the first lines

    Hello guys,

    I have a file.txt with 10 or more lines, data gets appended continuously but i want to maintain 10 lines in this text file at every time.
    I am looking at achieving this by checking the total lines immediately after inserting, if >10 then delete from line 1 to line (total-10)

    I have seen loads of ways to do this online especially with RandomAccessFile, i need a very efficient way of doing this. I wont be dealing with large data so am more concerned with the fastest way to do this.

    example, i want a file with content like this

    line1
    line2
    line3
    line4
    line5
    line6
    line7
    line8
    line9
    line10
    line11
    line12

    to become something like this

    line3
    line4
    line5
    line6
    line7
    line8
    line9
    line10
    line11
    line12


    Thanks in advance

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: I need to truncate a file with 10 or more lines, to 10, removing the first lines

    I would read each line into a string array using a FileReader, and then output the last 10 lines, by starting at array.size-10. You would have to use a different output file or overwrite your input file. That could be a problem if there are constant insertions.

  3. #3
    sledjama is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default Re: I need to truncate a file with 10 or more lines, to 10, removing the first lines

    this actually was the headache that has been running in my head for a while now

    it's actually a chatroom am trying to make available on mobile devices from a red5 chat session
    i have tried php-rtmp, amfphp, etc with no luck yet

    so i decided to listen to a file on server using php comet services
    now, i got red5 to populate the said file.txt, then serve it to mobile users with the listening clientside javascript
    but i need to maintain that the file on server remains at 10 lines for mobile users to access.

    One issue is while reading the file, deleting the lines, putting it back
    another message may have arrived and then getting overwritten. More why am looking at "fastest"


    I still intend to read specific lines that starts with a special predefined character, so i can display mobile chat messages to the red5 chat users
    am totally lost on this.


    is it not possible that i lock the file while doing this?

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: I need to truncate a file with 10 or more lines, to 10, removing the first lines

    Why don't you use a string queue and pop one off the top every time you insert one at the bottom.

Similar Threads

  1. I want to know how many lines are in a File I open
    By blackbird in forum New To Java
    Replies: 11
    Last Post: 07-22-2011, 10:02 PM
  2. Load file with a lot or lines.
    By juamd in forum Advanced Java
    Replies: 9
    Last Post: 04-26-2011, 07:41 PM
  3. how to remove duplicate lines from a file
    By trkece in forum New To Java
    Replies: 11
    Last Post: 02-16-2011, 06:01 AM
  4. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  5. Removing empty lines from code using Eclipse
    By javaplus in forum Eclipse
    Replies: 1
    Last Post: 12-14-2007, 09:21 PM

Posting Permissions

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