Deleting a File

Posted on 2006-11-02
Medium Priority
Last Modified: 2010-03-31

An operation I'm calling is creating a logfile outside of my program.  I am parsing that log file's information.  Once parsed, the file is useless and I want to delete it automatically.

Currently, this is how I'm doing it:

String filedelete = "del c:\\progout.txt";
try {
Process p = run.exec(filedelete);

But, the command prompt window pops up blank and just sits.

Any idea how to modify my command in String filedelete?

Much thanks!
Question by:LouAtColumn
  • 4
LVL 16

Accepted Solution

imladris earned 375 total points
ID: 17861592
Delete is (in DOS and Windows) a "built in" command. Built in to the command processor, that is. So, to go down that path you would have to set the string to something like "cmd /c del c:\\progout.txt".

However, that is, of course, operating system specific. Java does have mechanisms of its own to deal with files. Using them is probably preferable. Much of it involves the File class. Deleting could be done with something like:

import java.io.*;

File df=new File("c:\\progout.txt");


Author Comment

ID: 17862847
I tried the delete() method and it did not work, nor throw any exceptions.  I don't necessarily have to go down this road because this is a windows-only solution.

So, back to making my command work correctly, I'm still not getting any results...no delete and no errors.  I've put a thread sleep in just to put some space between the previous process and this delete action.

String dumpfile = "cmd /C del c:\\progout.txt";
      Process p2 = run.exec(dumpfile);
      catch (Exception ee)

Any other idears heres?


Author Comment

ID: 17862874
Points upped

Author Comment

ID: 17863074
Quick update.

The above code works sparadically (around 25% of the time).


Author Comment

ID: 17863249
Well, looks like I managed to figure this one out.

I moved the delete function out to the main function and all is 100% now.

Thank you for your help imladris!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question