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

Java File Handling परिचय

File handling यह है कि आपका program files और folders कैसे बनाता, जांचता, और delete करता है, एक filing cabinet में paper manage करने जैसा। Java के पास इसके लिए एक File tool है।
Syntax
java
File file = new File("file_name");
file.exists();
file.getName();

Creating a File

Java की File class filesystem पर एक path represent करती है — एक File object बनाना एक असली file तब तक नहीं बनाता जब तक आप createNewFile() जैसा एक method call न करें, जो false return करता है अगर उस path पर पहले से एक file मौजूद हो।

उदाहरण: Creating a File

java
import java.io.File;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		File file = new File("notes.txt"); // no file yet, just a path reference
		boolean created = file.createNewFile();
		System.out.println(created);
	}
}

File Properties and Metadata

एक File object metadata expose करता है जैसे length() (bytes में size), lastModified(), और canRead()/canWrite(), आपको इसे खोले बिना किसी file की properties inspect करने देते हुए।

उदाहरण: File Properties and Metadata

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("notes.txt");
		file.createNewFile();
		// Print a line to the console
		System.out.println(file.length());
		// Print a line to the console
		System.out.println(file.canRead());
	}
}

Writing check parameters

किसी file में लिखने से पहले, exists() जांचना और IOException handle करना worth है, क्योंकि file operations आपके program के control से बाहर कारणों से fail हो सकते हैं — permissions, missing directories, या एक full disk।

उदाहरण: Writing check parameters

java
// Import java.io.File so it can be used by its short name
import java.io.File;
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
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) {
		// Create a new File object and store it in file
		File file = new File("notes.txt");
		// Try code that might throw an exception
		try {
			// Check whether !file.exists()
			if (!file.exists()) {
				file.createNewFile();
			}
			// Create a new FileWriter object and store it in writer
			FileWriter writer = new FileWriter(file);
			writer.write("Hello");
			writer.close();
		} catch (IOException e) {
			System.out.println("Write failed: " + e.getMessage());
		}
	}
}

Deleting a File

delete() एक file (या एक empty directory) permanently हटाता है और success indicate करती एक boolean return करता है — यह throw करने के बजाय silently fail होता है, इसलिए अगर deletion मायने रखे तो हमेशा return value जांचें।

उदाहरण: Deleting a File

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("notes.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);
	}
}

Working with Directories

mkdir() एक single directory बनाता है जबकि mkdirs() path के साथ-साथ किसी भी missing parent directories भी बनाता है — mkdirs() उपयोग करें जब तक आप निश्चित न हों कि parent structure पहले से मौजूद है।

उदाहरण: Working with Directories

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File single = new File("folder1");
		single.mkdir();
		File nested = new File("folder2/subfolder");
		nested.mkdirs(); // creates missing parent too
		System.out.println(nested.exists());
	}
}
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. यह भूल जाना कि new File(...) disk पर एक file नहीं बनाता।
  2. यह उम्मीद करना कि new File("a.txt") fail होगा जब file गायब हो, जब यह सिर्फ एक object बनाता है और exists() जांचा जाना चाहिए।
  3. गलत path separators उपयोग करना, जब File.separator या Paths.get() safer है।

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.