Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

How can I empty a file?

Thread view: 
Ramon F Herrera - 28 Jul 2006 12:51 GMT
How can I accomplish something like this:

% cp /dev/null somefile

in Java??

Thanks,

-Ramon
Vincent van Beveren - 28 Jul 2006 13:26 GMT
> 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
Ramon F Herrera - 28 Jul 2006 21:00 GMT
> > How can I accomplish something like this:
> >
[quoted text clipped - 19 lines]
>
> 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
Oliver Wong - 28 Jul 2006 21:51 GMT
>> > How can I accomplish something like this:
>> >
[quoted text clipped - 25 lines]
> "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
Ramon F Herrera - 29 Jul 2006 02:35 GMT
> >> > How can I accomplish something like this:
> >> >
[quoted text clipped - 30 lines]
>
>     - Oliver

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

Thanks!

-Ramon


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.