End of File

Discussion in 'Java' started by Ken Kast, Apr 18, 2004.

  1. Ken Kast

    Ken Kast Guest

    I have a character file off of which I'd like to eliminate the last n chars.
    Put another way, when an app appends to the file I'd like it to append n
    chars from the end, with the last n chars going to the great bit bucket in
    the sky. Other than doing a read/write operation to eliminate the end of
    the file, is there any simple way to move the EOF up n chars?

    Thanks.

    Ken
    Ken Kast, Apr 18, 2004
    #1
    1. Advertising

  2. Ken Kast

    Roedy Green Guest

    On Sun, 18 Apr 2004 01:34:09 GMT, "Ken Kast" <>
    wrote or quoted :

    > Other than doing a read/write operation to eliminate the end of
    >the file, is there any simple way to move the EOF up n chars?


    There is in Windows using JNI. You open the file and do a 0-byte write
    where you want to chop then close. When Ray Duncan first told me
    about this I thought it was a joke. Otherwise, you have to copy up to
    the place you want then delete and rename.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 18, 2004
    #2
    1. Advertising

  3. Ken Kast wrote:
    > I have a character file off of which I'd like to eliminate the last n chars.
    > Put another way, when an app appends to the file I'd like it to append n
    > chars from the end, with the last n chars going to the great bit bucket in
    > the sky. Other than doing a read/write operation to eliminate the end of
    > the file, is there any simple way to move the EOF up n chars?
    >
    > Thanks.
    >
    > Ken
    >
    >


    I think you need to look at RandomAccessFile, which will allow you to seek
    to n bytes from the end.

    Steve
    Steve Horsley, Apr 18, 2004
    #3
  4. Steve Horsley wrote:

    >> I have a character file off of which I'd like to eliminate the last n
    >> chars.
    >> Put another way, when an app appends to the file I'd like it to append n
    >> chars from the end, with the last n chars going to the great bit
    >> bucket in
    >> the sky. Other than doing a read/write operation to eliminate the
    >> end of
    >> the file, is there any simple way to move the EOF up n chars?

    >
    > I think you need to look at RandomAccessFile, which will allow you to seek
    > to n bytes from the end.


    And also to shorten the file, which I think is what he actually wants to do.
    Michael Borgwardt, Apr 19, 2004
    #4
  5. Ken Kast

    Roedy Green Guest

    On Mon, 19 Apr 2004 12:04:19 +0200, Michael Borgwardt
    <> wrote or quoted :

    >And also to shorten the file, which I think is what he actually wants to do.


    RandomAccessFile.setLength. Never noticed that before.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 19, 2004
    #5
  6. Ken Kast

    Guest

    import java.io.*;
    public class JamEOF
    {
    RandomAccessFile rf;
    File f;
    public JamEOF() throws IOException
    {
    f = new File("c:\\windows\\desktop\\test.txt");
    rf = new RandomAccessFile(f , "rw");
    System.out.println("Before update: "+rf.length());
    long n=56;
    rf.setLength(rf.length() - n);
    System.out.println("After update: "+rf.length());
    rf.close();
    }

    public static void main(String [] args) throws IOException
    {
    new JamEOF();
    }
    }

    Have a good day.
    Peace.
    , Apr 20, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    2
    Views:
    3,593
    Versteijn
    Aug 18, 2004
  2. William
    Replies:
    2
    Views:
    1,728
    Kevin Spencer
    Jun 1, 2005
  3. Replies:
    2
    Views:
    353
  4. Jimmy
    Replies:
    3
    Views:
    325
    J. J. Farrell
    Sep 9, 2011
  5. Jimmy
    Replies:
    13
    Views:
    648
    Peter Nilsson
    Sep 21, 2011
Loading...

Share This Page