Solved

Deleting a File

Posted on 2006-11-02
5
198 Views
Last Modified: 2010-03-31
Hello,

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);
}
catch...


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


Any idea how to modify my command in String filedelete?



Much thanks!
0
Comment
Question by:LouAtColumn
  • 4
5 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 125 total points
Comment Utility
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");
df.delete();

0
 

Author Comment

by:LouAtColumn
Comment Utility
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";
      try
      {
      Thread.sleep(4000);
      Process p2 = run.exec(dumpfile);
      }
      catch (Exception ee)
      {
      ee.printStackTrace();
      }


Any other idears heres?

TIA
0
 

Author Comment

by:LouAtColumn
Comment Utility
Points upped
0
 

Author Comment

by:LouAtColumn
Comment Utility
Quick update.

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


Hmmmm.....
0
 

Author Comment

by:LouAtColumn
Comment Utility
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!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 41
Exchange and Third Party application. 8 59
Receive file in Servlet 1 36
return in catch statement 1 39
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now