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

Java में Files बनाना

डिस्क पर एक नई file या तो classic File class के createNewFile method या newer NIO Files.createFile method उपयोग करके बनाई जा सकती है, दोनों directories बनाने को भी support करते हैं।
Syntax
java
import java.io.File;

File file = new File("file_name");
file.createNewFile();

Creating a File with the File Class

डिस्क पर एक नई empty file File class के createNewFile method उपयोग करके बनाई जा सकती है, जो true return करता है अगर file असल में बनाई गई और false अगर उस path पर पहले से एक file मौजूद हो।

उदाहरण: Creating 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("output.txt");
		// Declare created and set it to file.createNewFile()
		boolean created = file.createNewFile();
		// Print a line to the console
		System.out.println(created);
	}
}

Checking if a File Already Exists

एक file बनाने से पहले, exists method उपयोग करके यह जांचना आम है कि यह पहले से मौजूद है या नहीं, क्योंकि पहले से वहां मौजूद एक file बनाने की कोशिश एक error का कारण बनने के बजाय बस कुछ नहीं करती।

उदाहरण: Checking if a File Already Exists

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("output.txt");
		// Check whether !file.exists()
		if (!file.exists()) {
			file.createNewFile();
		}
		// Print a line to the console
		System.out.println(file.exists());
	}
}

Creating Directories

Directories files से अलग एक single नई directory के लिए mkdir उपयोग करके बनाई जाती हैं, जो fail होता है अगर इसका parent मौजूद न हो, या parent directories की एक पूरी missing chain एक साथ बनाने के लिए mkdirs।

उदाहरण: Creating Directories

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File dir = new File("data/backup"); // parent missing
		System.out.println(dir.mkdir()); // fails: parent doesn't exist
		System.out.println(dir.mkdirs()); // creates whole chain
	}
}

Creating Files with NIO

Newer NIO.2 API, Path और Files utility class के आस-पास बनी, files और directories बनाने के लिए classic File class का एक ज़्यादा modern alternative createFile और createDirectories के रूप में प्रदान करती है।

उदाहरण: Creating Files with NIO

java
// Import java.nio.file.Files so it can be used by its short name
import java.nio.file.Files;
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
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 {
		Path path = Path.of("nio-output.txt");
		// Check whether !Files.exists(path)
		if (!Files.exists(path)) {
			Files.createFile(path);
		}
		// Print a line to the console
		System.out.println(Files.exists(path));
	}
}

Handling Creation Errors

File creation missing permissions या एक full disk जैसे कारणों से fail हो सकता है, और दोनों APIs इसे अलग तरीके से report करती हैं: classic File class false return करता है या IOException throw करता है, जबकि NIO API FileAlreadyExistsException जैसा एक specific exception throw करता है।

उदाहरण: Handling Creation Errors

java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
	public static void main(String[] args) {
		try {
			Files.createFile(Path.of("/nonexistent-dir/file.txt")); // NIO throws
		} catch (IOException e) {
			System.out.println("NIO threw: " + e.getClass().getSimpleName());
		}
	}
}
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. createNewFile() का result नज़रअंदाज़ करना, जो file पहले से मौजूद होने पर false return करता है।
  2. IOException handle करना भूल जाना, जो एक checked exception है।
  3. mkdir() उपयोग करना जब parent folders गायब हों, जब mkdirs() उन्हें बनाता है।

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.