Java में Files बनाना
In this page:
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Checking if a File Already Exists
एक file बनाने से पहले, exists method उपयोग करके यह जांचना आम है कि यह पहले से मौजूद है या नहीं, क्योंकि पहले से वहां मौजूद एक file बनाने की कोशिश एक error का कारण बनने के बजाय बस कुछ नहीं करती।
उदाहरण: Checking if a File Already Exists
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
Creating Directories
Directories files से अलग एक single नई directory के लिए mkdir उपयोग करके बनाई जाती हैं, जो fail होता है अगर इसका parent मौजूद न हो, या parent directories की एक पूरी missing chain एक साथ बनाने के लिए mkdirs।
उदाहरण: Creating Directories
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
createNewFile()का result नज़रअंदाज़ करना, जो file पहले से मौजूद होने परfalsereturn करता है।IOExceptionhandle करना भूल जाना, जो एक checked exception है।mkdir()उपयोग करना जब parent folders गायब हों, जबmkdirs()उन्हें बनाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: