How can I empty a file?

Discussion in 'Java' started by Ramon F Herrera, Jul 28, 2006.

  1. How can I accomplish something like this:

    % cp /dev/null somefile

    in Java??

    Thanks,

    -Ramon
    Ramon F Herrera, Jul 28, 2006
    #1
    1. Advertising

  2. > How can I accomplish something like this:
    >
    > % cp /dev/null somefile
    >
    > in Java??
    >


    RandomAccessFile raf = new RandomAccessFile("somefile", "rw");
    raf.setLength(0);
    raf.close();

    or

    (new FileOutputStream("somefile")).close();

    or

    File f = new File("somefile");
    if (f.exists()) {
    f.delete();
    }
    f.createNewFile();

    Vincent
    Vincent van Beveren, Jul 28, 2006
    #2
    1. Advertising

  3. Vincent van Beveren wrote:
    > > How can I accomplish something like this:
    > >
    > > % cp /dev/null somefile
    > >
    > > in Java??
    > >

    >
    > RandomAccessFile raf = new RandomAccessFile("somefile", "rw");
    > raf.setLength(0);
    > raf.close();
    >
    > or
    >
    > (new FileOutputStream("somefile")).close();
    >
    > or
    >
    > File f = new File("somefile");
    > if (f.exists()) {
    > f.delete();
    > }
    > f.createNewFile();
    >
    > Vincent


    Vince:

    Thanks for your answer. I should have specified that the file
    "somefile" already exists but I don't care about its contents, I just
    need to empty it.

    -Ramon
    Ramon F Herrera, Jul 28, 2006
    #3
  4. Ramon F Herrera

    Oliver Wong Guest

    "Ramon F Herrera" <> wrote in message
    news:...
    >
    > Vincent van Beveren wrote:
    >> > How can I accomplish something like this:
    >> >
    >> > % cp /dev/null somefile
    >> >
    >> > in Java??
    >> >

    >>
    >> RandomAccessFile raf = new RandomAccessFile("somefile", "rw");
    >> raf.setLength(0);
    >> raf.close();
    >>
    >> or
    >>
    >> (new FileOutputStream("somefile")).close();
    >>
    >> or
    >>
    >> File f = new File("somefile");
    >> if (f.exists()) {
    >> f.delete();
    >> }
    >> f.createNewFile();
    >>
    >> Vincent

    >
    > Vince:
    >
    > Thanks for your answer. I should have specified that the file
    > "somefile" already exists but I don't care about its contents, I just
    > need to empty it.


    Did you try the suggestions above? I think they would work despite this
    extra specification.

    - Oliver
    Oliver Wong, Jul 28, 2006
    #4
  5. Oliver Wong wrote:
    > "Ramon F Herrera" <> wrote in message
    > news:...
    > >
    > > Vincent van Beveren wrote:
    > >> > How can I accomplish something like this:
    > >> >
    > >> > % cp /dev/null somefile
    > >> >
    > >> > in Java??
    > >> >
    > >>
    > >> RandomAccessFile raf = new RandomAccessFile("somefile", "rw");
    > >> raf.setLength(0);
    > >> raf.close();
    > >>
    > >> or
    > >>
    > >> (new FileOutputStream("somefile")).close();
    > >>
    > >> or
    > >>
    > >> File f = new File("somefile");
    > >> if (f.exists()) {
    > >> f.delete();
    > >> }
    > >> f.createNewFile();
    > >>
    > >> Vincent

    > >
    > > Vince:
    > >
    > > Thanks for your answer. I should have specified that the file
    > > "somefile" already exists but I don't care about its contents, I just
    > > need to empty it.

    >
    > Did you try the suggestions above? I think they would work despite this
    > extra specification.
    >
    > - Oliver


    Yes, I tried them, and they work as expected.

    Thanks!

    -Ramon
    Ramon F Herrera, Jul 29, 2006
    #5
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,022
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    802
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    572
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    657
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    551
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page