Java File Handling परिचय
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
File Properties and Metadata
एक File object metadata expose करता है जैसे length() (bytes में size), lastModified(), और canRead()/canWrite(), आपको इसे खोले बिना किसी file की properties inspect करने देते हुए।
उदाहरण: File Properties and Metadata
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
Writing check parameters
किसी file में लिखने से पहले, exists() जांचना और IOException handle करना worth है, क्योंकि file operations आपके program के control से बाहर कारणों से fail हो सकते हैं — permissions, missing directories, या एक full disk।
उदाहरण: Writing check parameters
// 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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Deleting a File
delete() एक file (या एक empty directory) permanently हटाता है और success indicate करती एक boolean return करता है — यह throw करने के बजाय silently fail होता है, इसलिए अगर deletion मायने रखे तो हमेशा return value जांचें।
उदाहरण: Deleting a File
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Working with Directories
mkdir() एक single directory बनाता है जबकि mkdirs() path के साथ-साथ किसी भी missing parent directories भी बनाता है — mkdirs() उपयोग करें जब तक आप निश्चित न हों कि parent structure पहले से मौजूद है।
उदाहरण: Working with Directories
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());
}
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
new File(...)disk पर एक file नहीं बनाता। - यह उम्मीद करना कि
new File("a.txt")fail होगा जब file गायब हो, जब यह सिर्फ एक object बनाता है औरexists()जांचा जाना चाहिए। - गलत path separators उपयोग करना, जब
File.separatorयाPaths.get()safer है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: