Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to delete a file) ~ foundjava



Click here to watch in Youtube :
FileDemo1.java
import java.io.File;

public class FileDemo1
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/myfile.txt");
        /*
         * Deletes the file or directory denoted by this
         * abstract pathname.
         * 
         * Returns: true if and only if the file or
         * directory is successfully deleted; false
         * otherwise
         */
        boolean success = file.delete();
        System.out.println("success = " + success);
    }

}
Output
success = true
FileDemo2.java
import java.io.File;

public class FileDemo2
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/misc");
        /*
         * Deletes the file or directory denoted by this
         * abstract pathname.
         * 
         * Returns: true if and only if the file or
         * directory is successfully deleted; false
         * otherwise
         */
        boolean success = file.delete();
        System.out.println("success = " + success);
    }

}
Output
success = true
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment