Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I empty a file?

Reply
Thread Tools

How can I empty a file?

 
 
Ramon F Herrera
Guest
Posts: n/a
 
      07-28-2006

How can I accomplish something like this:

% cp /dev/null somefile

in Java??

Thanks,

-Ramon

 
Reply With Quote
 
 
 
 
Vincent van Beveren
Guest
Posts: n/a
 
      07-28-2006
> 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
 
Reply With Quote
 
 
 
 
Ramon F Herrera
Guest
Posts: n/a
 
      07-28-2006

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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-28-2006

"Ramon F Herrera" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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

 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      07-29-2006

Oliver Wong wrote:
> "Ramon F Herrera" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments