Showing posts with label file handling in Java. Show all posts
Showing posts with label file handling in Java. Show all posts

Monday, August 4, 2025

Creation of Files in Java जावा में फाइल निर्माण

File creation is an essential part of file handling in Java. We use the `File` class from the java.io package to create new files.

जावा में फाइल हैंडलिंग का एक महत्वपूर्ण हिस्सा फाइल निर्माण है। हम नई फाइल बनाने के लिए java.io पैकेज की `File` क्लास का उपयोग करते हैं।

Key Points / मुख्य बिंदु:-

1. Use File class to create new files.

   नई फाइल बनाने के लिए File क्लास का उपयोग करें।

2. `createNewFile()` method returns true if the file is created successfully.

   `createNewFile()` मेथड true लौटाता है यदि फाइल सफलतापूर्वक बनती है।

3. If the file already exists, the method returns false.

   यदि फाइल पहले से मौजूद है, तो मेथड false लौटाता है।


💻 Example 1: Create a File

import java.io.File;

import java.io.IOException;

public class CreateFileExample {

    public static void main(String[] args) {

        try {

            File file = new File("myfile.txt");

            if (file.createNewFile()) {

                System.out.println("File created: " + file.getName());

            } else {

                System.out.println("File already exists.");

            }

        } catch (IOException e) {

            System.out.println("An error occurred.");

            e.printStackTrace();

        }

    }

}

Output 1 (If file does not exist):-

File created: myfile.txt


Output 2 (If file already exists):-

File already exists.


💻 Example 2: Check File Details After Creation

import java.io.File;

public class FileDetails {

    public static void main(String[] args) {

        File file = new File("myfile.txt");

        if (file.exists()) {

            System.out.println("File Name: " + file.getName());

            System.out.println("Absolute Path: " + file.getAbsolutePath());

            System.out.println("Writable: " + file.canWrite());

            System.out.println("Readable: " + file.canRead());

            System.out.println("File Size in bytes: " + file.length());

        } else {

            System.out.println("The file does not exist.");

        }

    }

}


Possible Output:-

File Name: myfile.txt

Absolute Path: C:\Users\Admin\myfile.txt

Writable: true

Readable: true

File Size in bytes: 0


🔹 Real-life Uses / वास्तविक उपयोग:-

* Storing logs in applications

  एप्लिकेशन में लॉग्स स्टोर करने के लिए

* Generating reports or text files dynamically

  डायनामिक रिपोर्ट्स या टेक्स्ट फाइल्स बनाने के लिए

* Temporary file creation for backup or caching

  बैकअप या कैशिंग के लिए अस्थायी फाइल निर्माण

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.
    फाइल की प्रॉपर्टीज जैसे साइज़, नाम या परमिशन को पढ़ना

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...