← Back to Core Java Course | Chapter 12: File I/O | Lesson 4 of 9

Java में Files हटाना

एक file डिस्क से या तो classic File class के delete method, जो एक boolean return करता है, या newer NIO Files.delete method, जो failure पर एक specific exception throw करता है, उपयोग करके हटाई जा सकती है।
Syntax
java
File file = new File("file_name");
file.delete();

Deleting a File with the File Class

एक file File class के delete method उपयोग करके डिस्क से हटाई जा सकती है, जो उस path पर file delete करने की कोशिश करता है और एक boolean return करता है indicate करते हुए कि deletion असल में succeed हुआ या नहीं।

उदाहरण: Deleting a File with the File Class

java
// Import java.io.File so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new File object and store it in file
		File file = new File("temp.txt");
		file.createNewFile();
		// Declare deleted and set it to file.delete()
		boolean deleted = file.delete();
		// Print a line to the console
		System.out.println(deleted);
	}
}

Checking Deletion Success

Delete method failure पर कभी एक exception throw नहीं करता -- यह चुपचाप false return करता है चाहे file पहले से मौजूद ही न रही हो या deletion permissions issue जैसे किसी और कारण से fail हुई हो।

उदाहरण: Checking Deletion Success

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File file = new File("does-not-exist.txt");
		boolean deleted = file.delete(); // false, no exception thrown
		System.out.println(deleted);
	}
}

Deleting Directories

वही delete method empty directories भी हटाता है, लेकिन यह fail होगा और false return करेगा अगर directory में अब भी कोई files या subdirectories हों, क्योंकि यह recursively delete नहीं करता।

उदाहरण: Deleting Directories

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File dir = new File("empty-folder");
		dir.mkdir();
		System.out.println(dir.delete()); // succeeds: directory is empty
	}
}

Deleting Files with NIO

NIO Files utility class delete प्रदान करती है, जो failure पर एक specific exception throw करता है, और deleteIfExists, जो throw करने के बजाय सुरक्षित रूप से false return करता है जब target file बस वहां न हो।

उदाहरण: Deleting Files with NIO

java
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		Path path = Path.of("temp.txt");
		Files.createFile(path);
		Files.delete(path); // throws on failure
		System.out.println(Files.deleteIfExists(path)); // false: already gone
	}
}

Handling Deletion Errors

Delete failures को अच्छी तरह handle करने का मतलब है success मानने के बजाय API जो boolean या exception देता है उसे जांचना, क्योंकि एक failed deletion अक्सर इशारा करता है कि file कहीं और खुली है या file system permissions की कमी है।

उदाहरण: Handling Deletion Errors

java
// Import java.io.File so it can be used by its short name
import java.io.File;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new File object and store it in file
		File file = new File("temp.txt");
		// Check whether !file.delete()
		if (!file.delete()) {
			// Print a line to the console
			System.out.println("Deletion failed: check permissions or existence");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. File.delete() से returned boolean नज़रअंदाज़ करना, इसलिए एक failure unnoticed रह जाती है।
  2. एक non-empty directory delete करने की कोशिश करना, जो fail होता है।
  3. एक गलत path उपयोग करना, जो report करता है कि file नहीं मिली।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.