How would I erase the beginning of a file?

I want to be able to erase the beginning of a file, while writing to the end...WITHOUT reading the whole file and writing it again because that would be a waste of resourses. Obviously I know that option is available.
My situation is that I am adding lines to a log file, and I want to remove any entries that are X hours old. Logging a lot of information would mean that every few seconds, an entry is older than X hours and needs to be removed. It doesnt have a set number of lines so I can't just recycle the file by doing something like: if at line number Y, start at line 1 and remember the current line number.
Adding to the top of the file, pushing the others lines down, and making cuts at the end would also work, but the same situation arises, how do I add to the beginning of the file, without having to copy the entire file to add the entry to the top.
The reason I don't want to read the whole file is because having to read the whole file and write the whole file when an entry is made is time consuming and wastes CPU and memory, especially when the logs are coming in several lines a second.

I would think it could easily be done. say you want to erase the first 10 bytes, why cant you just say move the start of the file up 10 bytes. Somewhere in the filesystem the start of the file is stored. I found underlying file copying that claims this in FileChannel's transferTo and transferFrom:
This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.
If I can't just chop, I'll have to copy quickly at scheduled intervals.
This is code I ran to test the above quote:
    public static void main(String[] args) throws Exception{
        args = new String[2];
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter File Name:");
        args[0] = input.readLine();
        System.out.println("Enter test (0-2):");
        args[1] = input.readLine();
        long time = System.currentTimeMillis();
        int version = Integer.parseInt(args[1]);
        switch (version) {
            case 0:
                BufferedReader in = new BufferedReader(new FileReader(args[0]));
                PrintWriter out = new PrintWriter(new FileWriter("tempfile.txt"));
                in.readLine();//skip line.
                for (String line = in.readLine();line != null;line = in.readLine()) {
                    out.println(line);
                in.close();
                out.close();
                break;
            case 1:
                RandomAccessFile raf = new RandomAccessFile(args[0],"rw");
                String line = raf.readLine();
                long offset = raf.getFilePointer();
                byte[] b = new byte[1024];
                int bytes = 1024;
                long index = 0;
                System.out.println("index = " + raf.getFilePointer());
                System.out.println("index = " + line.length());
                while(bytes == 1024) {
                    bytes = raf.read(b);
                    index = raf.getFilePointer();
                    raf.seek(index - bytes - offset);
                    raf.write(b,0,bytes);
                    raf.seek(index);
                raf.setLength(raf.length()-offset);
                raf.close();
                break;
            case 2:
                RandomAccessFile raf2 = new RandomAccessFile(args[0],"rw");
                String line2 = raf2.readLine();
                long offset2 = raf2.getFilePointer();
                FileChannel f = raf2.getChannel();
                FileChannel g = f.position(0);
                g.transferTo(offset2,raf2.length() - offset2,f);
                raf2.setLength(raf2.length()-offset2);
                raf2.close();
                break;
        System.out.println(System.currentTimeMillis() - time);
    }and had these results
166MB Log File
Linux Mandrake 900Mhz
Case 0 = 32914 ms
Case 1 (1k byte[]) = 15043 ms
Case 1 (16k byte[]) = 6988 ms
Case 1 (1MB byte[] = 6113 ms
Case 2 = 6188 ms
:-/ guess this proves that the FileChannel can't chop

Similar Messages

  • How do I erase the hard drive on my 2007 Macbook with operating system 10.4.11 ?

    I am going to recycle my old Macbook and want to erase the hard drive. When I go into utilities-disk utility-click on Macintosh HD-and the Erase tab, the only active button is Erase Free Space. The Security Options and Erase button are not active. How can I erase the entire hard drive? The operating system is 10.4.11.

    You have to boot to another drive.  You cannot erase the drive to which you are booted.  Typically you would use the system installer discs that were provided with the computer originally.
    Alternatively you can use another computer which is running OSX and put the old one in Target Disk Mode.  The old one will simply be seen as a normal hard drive which you can erase.
    How to use FireWire target disk mode - http://support.apple.com/kb/HT1661 - includes description of hardware and software requirements. [What to do if your Mac doesn't enter FireWire Target Disk Mode|http://docs.info.apple.com/article.html?artnum=75414], also read about [Open Firmware Password Protection|http://www.apple.com/support/downloads/openfirmwarepassword.html] which can disable Target Disk Mode.

  • With Lion, How Can I Erase the Hard Drive and Reinstall The System Software?

    I plan on buying a used iMac with a Core 2 Duo so that I can run Lion.  Since Lion can only be purchased at the Mac App store what if I needed to erase my hard drive and re-install the system software.  How can I do this without a DVD with the OS on it like I have for Snow Leapard.  I suppose that I could boot from my Snow Leapard DVD and do the erase.  Then, I supposed I would be able to download Lion again from the Mac App Store without paying for it again.  But, how would you erase a hard drive and reinstall the system software when purchasing a new Mac with Lion already installed and no DVD at all with the OS on it?
    -- Kaydell

    When someone purchases any Apple computer with a DVD Drive, they should have recieved two DVDs... One contains the Operating System (up to Snow Leopard, Lion no longer ships on DVD) and the other contains the SYSTEM SOFTWARE (i.e. iLife and iWork). To install the software, they just insert the DVD with the SYSTEM SOFTWARE into any compatible Mac and run the installer. There is no need to erase the hard drive to install SYSTEM SOFTWARE. Maybe a friend can let you borrow the SYSTEM SOFTWARE DVD. The other option is to purchase the software via the App Store.
    FYI... App Store purchases are linked to the PURCHASER'S APPLE ID. So if you buy a used Mac from a friend and that Mac already has Lion installed on it,that Lion purchase is linked to YOUR FRIEND'S APPLE ID, not yours. So if you want to download Lion again, you will have to enter your FRIEND'S APPLE ID AND PASSWORD. You can always have your friend re-download Lion and make you a DVD copy.

  • Firefox memeorised my username and password with a shared laptop. How do i erase the memorised information?

    Firefox memeorised my username and password with a shared laptop. How do i erase the memorised information?

    Hello if you go to the tools at the top of the screen and click on it.
    Then you go to the options in that list and click on it.
    Then you select security its the padlock its the last but one in the list click on it.
    In that list you will have the options for the saved passwords click on this and then you can remove them.
    please note this is not a secure way to save your pass words.
    i would not use this.
    if you would like to save pass words try [http://passwordsafe.sourceforge.net/ and its free.
    hope this helps

  • How can i erase the firmware of cisco 7912 phone ?

    how can i erase the firmware of cisco 7912 phone ? the firmware is crashed in this 7912 phone , i want it to download another copy from CUCM8.5 .
    the way i did on 7911phone doesn't work on 7912.

    https://supportforums.cisco.com/discussion/10797901/reset-factory-default-ip-phone-7912
    You can try that procedure but I'm not sure how far that factory reset goes on those phones.  I know the 7940/60 series you can blow out everything except the CNU which would do exactly what you are looking for.

  • How can i erase  the account in the Macbook??

    How can i erase the account in the Macbook since i don't have the administrator's name and password, because whenever i install a new new software into the Macbook, it ask me for the administrator's name and password, but i don't have it and i didn't set it up. how can i erase the account and gain the access to the keychain setting?
    thank you so much for helping

    You would need to have the administrator give you admin privileges to be able to do this.

  • I recently installed 10.6.8 on my Imac and now i find i have 2 startup discs which i don't want. How do i erase the old startup disc which uses 10.5.8?

    I recently installed 10.6.8 on my Imac and now i find i have 2 startup discs which i don't want. How do i erase the old startup disc which uses 10.5.8?
    My Imac info is :
    It also appears that many of my files are duplicated.
    Work files and system files such as Library (i have 3 Libraries now..?)
    Also keep geting messages such as "scratch disks full".
    Any advice is welcome..

    Well I would recommend you do have an off internal hard drive backup of all your files before wiping any duplicates off the internal.  Once you are certain you have an off internal hard drive backup of all essential files, then use Disk Utility to erase the other partitions.

  • How do i erase the hard drive and reinstall?

    How do i erase the hard drive and reinstall everything? My computer is running slow and i thought with having a 2.4ghz processor and 4gb of ram would make speed not a factor but obviously i was wrong in that my comp is running slow. Not as slow as my pentium processor desktop but its too slow for my tastes. Im waiting to get an external hd so i can save my songs and pictures to before i do erase everything.

    Go to finder and in Spotlight, type in erase and install. The second choice is "Reinstall OS X. Print that out and follow the directions.
    Good Luck.
    Adam

  • I HAVE AN IPHONE 5S AND WANT TO USE SOFTWARE 7.0.2 BECAUSE IT IS COMPATIBLE WITH MY VEHICLE FOR UCONNECT. I CURRENTLY HAVE SOFTWARE 7.0.3 ON MY DEVICE. HOW WOULD I GET THE PREVIOUS SOFTWARE BACK ON MY PHONE

    I HAVE AN IPHONE 5S AND WANT TO USE SOFTWARE 7.0.2 BECAUSE IT IS COMPATIBLE WITH MY VEHICLE FOR UCONNECT. I CURRENTLY HAVE SOFTWARE 7.0.3 ON MY DEVICE. HOW WOULD I GET THE PREVIOUS SOFTWARE BACK ON MY PHONE

    Forum Tip: It is generally considered inappropriate to type all in uppercase letters in Internet discussions, as text that is typed all in uppercase is by convention considered to be shouting.  Uppercase is also more difficult to read, so please use all uppercase sparingly and only when you really mean to shout, which we hope you won’t need to do here .
    Regards.

  • Can I set up a new Mac Pro late 2013 using a Wacom Cintique 13HD tablet as the default monitor? How would I install the driver software for the tablet?

    Can I set up a new Mac Pro late 2013 using a Wacom Cintique 13HD tablet as the default monitor? How would I install the driver software for the tablet?

    The wacom site will have the drivers for you.
    http://us.wacom.com/en/support/drivers

  • I need to know how to edit a drawing - basically remove the arrows and text but at the same time match the background of the existing pic. then re add new text. how do i erase the arrows and text and arrows but match current background of pic. step by ste

    i need to know how to edit a drawing - basically remove the arrows and text but at the same time match the background of the existing pic. then re add new text. how do i erase the arrows and text and arrows but match current background of pic. step by step explanation please beginner

    Please post (a relevant section of) it right on this Forum.

  • How can I erase the data on my 2009 macbook pro that won't turn on because of a faulty motherboard? Do I just remove the hard drive, install it in a working mac, and erase it that way?

    How can I erase the data on my 2009 macbook pro that won't turn on because of a faulty motherboard? Do I just remove the hard drive, install it in a working mac, and erase it that way? Thanks.

    You put the internal drive in an enclosure. If you choose to keep the drive, you don't need to erase it.
    (110687)

  • How do I erase the "other" files that are occupying my computer

    I already deleted pics, music, etc and I am ready to continue to delete unnecessary files. How should I erase the "other" files that are in my computer. Please, advice. Thanks

    For information about the Other category in the Storage display, see this support article. If the Storage display seems to be inaccurate, try rebuilding the Spotlight index.
    Empty the Trash if you haven't already done so. If you use iPhoto, empty its internal Trash first:
              iPhoto ▹ Empty Trash
    Do the same in other applications, such as Aperture, that have an internal Trash feature. Then restart the computer. That will temporarily free up some space.
    According to Apple documentation, you need at least 9 GB of available space on the startup volume (as shown in the Finder Info window) for normal operation—not the mythical 10%, 15%, or any other percentage. You also need enough space left over to allow for growth of the data. There is little or no performance advantage to having more available space than the minimum Apple recommends. Available storage space that you'll never use is wasted space.
    See this support article for some simple ways to free up storage space.
    You can more effectively use a tool such as OmniDiskSweeper (ODS) or GrandPerspective (GP) to explore the volume and find out what's taking up the space. You can also delete files with it, but don't do that unless you're sure that you know what you're deleting and that all data is safely backed up. That means you have multiple backups, not just one. Note that ODS only works with OS X 10.8 or later. If you're running an older OS version, use GP.
    Deleting files inside an iPhoto or Aperture library will corrupt the library. Any changes to a photo library must be made from within the application that created it. The same goes for Mail files.
    Proceed further only if the problem isn't solved by the above steps.
    ODS or GP can't see the whole filesystem when you run it just by double-clicking; it only sees files that you have permission to read. To see everything, you have to run it as root.
    Back up all data now.
    If you have more than one user account, make sure you're logged in as an administrator. The administrator account is the one that was created automatically when you first set up the computer.
    Install the app you downloaded in the Applications folder as usual. Quit it if it's running.
    Triple-click anywhere in the corresponding line of text below on this page to select it, then copy the selected text to the Clipboard by pressing the key combination command-C:
    sudo /Applications/OmniDiskSweeper.app/Contents/MacOS/OmniDiskSweeper
    sudo /Applications/GrandPerspective.app/Contents/MacOS/GrandPerspective
    Launch the built-in Terminal application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Terminal in the icon grid.
    Paste into the Terminal window by pressing command-V. You'll be prompted for your login password, which won't be displayed when you type it. Type carefully and then press return. You may get a one-time warning to be careful. If you see a message that your username "is not in the sudoers file," then you're not logged in as an administrator. Ignore any other messages that appear in the Terminal window.
    The application window will open, eventually showing all files in all folders, sorted by size. It may take a few minutes for the app to finish scanning.
    I don't recommend that you make a habit of doing this. Don't delete anything as root. If something needs to be deleted, make sure you know what it is and how it got there, and then delete it by other, safer, means. When in doubt, leave it alone or ask for guidance.
    When you're done with the app, quit it and also quit Terminal.

  • How do I erase the hard drive on my mac mini with OS X lion

    How do I erase the hard drive on my mac mini with os x lion

    Restart with the Command and R keys held down and use the Disk Utility from there.
    (127020)

  • How I can erase the components that I have created in a content area?

    hello. Somebody knows how I can erase the components that I have created in a content area. I have created an area of content and the archives, categorias or perspective that I generate not let erase. The same it happens to me to the forms, reports and other components created under a certain application

    goto portal navigator --> content areas --> next to the content area --> click Edit Properties --> Items tab --> last button on the page is Purge..
    this should take care of it

Maybe you are looking for