File delete() method in Java with Examples Last Updated : 28 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The delete() function is a part of File class in Java . This function deletes an existing file or directory. If the file is deleted then the function returns true else returns false Function signature: public boolean delete() Syntax: boolean var = file.delete(); Parameters: This method does not accept any parameter. Return Type: The function returns boolean data type representing whether the new file is deleted or not. Exception: This method throws Security Exception: if the write access to the file is denied. Below programs illustrates the use of delete() function: Example 1: The file "F:\\program.txt" is a existing file in F: Directory. Java // Java program to demonstrate // delete() method of File Class import java.io.*; public class solution { public static void main(String args[]) { try { // Get the file File f = new File("F:\\program.txt"); // delete file if (f.delete()) System.out.println("File deleted"); else System.out.println("File was not deleted"); } catch (Exception e) { System.err.println(e); } } } Output: File deleted Example 2: The file "F:\\program1.txt" does not exist Java // Java program to demonstrate // delete() method of File Class import java.io.*; public class solution { public static void main(String args[]) { try { // Get the file File f = new File("F:\\program1.txt"); // delete file if (f.delete()) System.out.println("File deleted"); else System.out.println("File was not deleted"); } catch (Exception e) { System.err.println(e); } } } Output: File was not deleted Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file. Comment More infoAdvertise with us Next Article File delete() method in Java with Examples andrew1234 Follow Improve Article Tags : Java Java-Functions java-file-handling Java-IO package Java-File Class +1 More Practice Tags : Java Similar Reads Files delete() method in Java with Examples The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If th 3 min read Files deleteIfExists() method in Java with Examples The deleteIfExists() method of java.nio.file.Files help us to delete a file if the file exists at the path. we pass the path of the file as a parameter to this method. This method will return true if the file was deleted by this method; false if the file could not be deleted because it did not exist 3 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read File createNewFile() method in Java with Examples The createNewFile() function is a part of File class in Java . This function creates new empty file. The function returns true if the abstract file path does not exist and a new file is created. It returns false if the filename already exists. Function signature: public boolean createNewFile() Synta 2 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read FileInputStream close() Method in Java with Examples FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. FileInputStream.close() method After any operation to the file, w 2 min read KeyStore deleteEntry() method in Java with Examples The deleteEntry(String alias) method of java.security.KeyStore class is used to remove the entry for requested alias name. Syntax: public final void deleteEntry(String alias) throws KeyStoreException Parameter: This method seeks the name of the alias as a parameter of String type of which the entry 3 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read List clear() method in Java with Examples The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it just removes all of the elements from the List. Example:Java// Java Program to Demonstrate // List clear() import java.util.*; class 2 min read Buffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example: buf.clear(); // Prepa 3 min read Like