Java File Class जावा फाइल क्लास

The File class in Java is part of the java.io package and is used to represent the path of files and directories. The File class does not read or write data directly; it only provides information about files and directories and methods to create, delete, or check properties.

जावा में File क्लास java.io पैकेज का हिस्सा है और इसका प्रयोग फाइल एवं डायरेक्टरी के पाथ को प्रदर्शित करने के लिए किया जाता है। File क्लास डेटा को सीधे पढ़ती या लिखती नहीं है; यह केवल फाइल और डायरेक्टरी की जानकारी और उन्हें बनाने, हटाने या चेक करने की सुविधा देती है।


🔹 Constructors of File Class (कन्स्ट्रक्टर्स)

  1. File(String pathname) – Creates a new File instance using a given pathname.
    File(String pathname) – दिये गये पाथनेम के आधार पर नई File ऑब्जेक्ट बनाता है।

  2. File(String parent, String child) – Creates a File object from directory and filename separately.
    File(String parent, String child) – डायरेक्टरी और फाइल का नाम अलग-अलग देकर File ऑब्जेक्ट बनाता है।


🔹 Common Methods of File Class (महत्वपूर्ण मेथड्स)

Method Description (English) विवरण (Hindi)
createNewFile() Creates a new empty file. नई खाली फाइल बनाता है।
exists() Checks if file/directory exists. फाइल या डायरेक्टरी का अस्तित्व जाँचता है।
delete() Deletes the file or directory. फाइल या डायरेक्टरी हटाता है।
getName() Returns the file name. फाइल का नाम लौटाता है।
getAbsolutePath() Returns the complete path of the file. फाइल का पूरा पाथ लौटाता है।
canRead() Checks if the file is readable. फाइल पढ़ी जा सकती है या नहीं, चेक करता है।
canWrite() Checks if the file is writable. फाइल लिखी जा सकती है या नहीं, चेक करता है।
length() Returns the file size in bytes. फाइल का साइज़ (बाइट्स में) लौटाता है।
mkdir() Creates a new directory. नई डायरेक्टरी बनाता है।

✅ Java Example: Create, Check, and Get File Info

import java.io.*;

public class FileExample {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");

            // Create new file if not exists
            if (file.createNewFile()) {
                System.out.println("File created: " + file.getName());
            } else {
                System.out.println("File already exists.");
            }

            // Display file information
            System.out.println("Absolute Path: " + file.getAbsolutePath());
            System.out.println("Writable: " + file.canWrite());
            System.out.println("Readable: " + file.canRead());
            System.out.println("File Size: " + file.length() + " bytes");
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Sample Output

File created: example.txt
Absolute Path: C:\Users\...\example.txt
Writable: true
Readable: true
File Size: 0 bytes

🔹 Real-life Uses of File Class (वास्तविक उपयोग)

  1. Check if a configuration file exists before using it.
    किसी कॉन्फ़िगरेशन फाइल का अस्तित्व उपयोग से पहले जाँचना।

  2. Create log files in applications.
    एप्लिकेशन में लॉग फाइल्स बनाना

  3. Read and process file properties like size, name, or permissions.
    फाइल की प्रॉपर्टीज जैसे साइज़, नाम या परमिशन को पढ़ना

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध