This program deletes a specified file or directory. This program is working fine just copy it and run, this will work fine.
import java.io.*;
public class DeleteFile
{
public static void deleteFile(String varFileName)
{
// Create a File object to represent the filename
File varFilePath = new File(varFileName);
// Make sure the file or directory exists
if (!varFilePath.exists())
System.out.println("Delete: no such file or directory: " +varFileName);
// Make sure the file or directory isn't write protected
if (!varFilePath.canWrite())
System.out.println("Delete: write protected: " + varFileName);
// If it is a directory, make sure it is empty
if (varFilePath.isDirectory())
{
//Get file list in a specidied directory
String[] files = varFilePath.list();
if (files.length > 0)
System.out.println("Delete: directory not empty: " + varFileName);
}
// If we passed all the tests, then attempt to delete it
boolean success = varFilePath.delete();
//If unable to delete
if (!success)
System.out.println("Delete: deletion failed");
}
public static void main(String[] args)
{
try
{
DeleteFile objDeleteFile = new DeleteFile ();
//deleteFile Function
objDeleteFile.deleteFile("c:\demo.txt");
}
catch(Exception e)
{
System.err.println("Error is " + e);
}
}
}
Subscribe to:
Post Comments (Atom)
1 comment:
nice code.... but seems to be a very general solution
Post a Comment