Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना

In Java, we can merge multiple files into a single file (concatenation) and use buffering to improve file I/O performance.

जावा में हम कई फाइलों को एक ही फाइल में जोड़ सकते हैं (कंकैटिनेशन) और बफरिंग का उपयोग करके फाइल I/O की गति बढ़ा सकते हैं।


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

  1. Concatenation: Combining multiple files into one.
    कंकैटिनेशन: कई फाइलों को एक फाइल में जोड़ना।

  2. Buffering: Using a buffer to reduce disk access and improve speed.
    बफरिंग: बफर का उपयोग करके डिस्क एक्सेस कम करना और गति बढ़ाना।

  3. Streams Used: SequenceInputStream, BufferedInputStream, BufferedOutputStream.
    उपयोग की गई स्ट्रीम्स: SequenceInputStream, BufferedInputStream, BufferedOutputStream


💻 Example 1: Concatenating Two Files

import java.io.*;

public class ConcatFiles {
    public static void main(String[] args) {
        try {
            FileInputStream fis1 = new FileInputStream("file1.txt");
            FileInputStream fis2 = new FileInputStream("file2.txt");

            SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
            FileOutputStream fos = new FileOutputStream("merged.txt");

            int data;
            while ((data = sis.read()) != -1) {
                fos.write(data);
            }

            sis.close();
            fis1.close();
            fis2.close();
            fos.close();

            System.out.println("Files concatenated successfully!");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Input Files:
file1.txt → Hello
file2.txt → World

Output File (merged.txt):

Hello World

💻 Example 2: Buffering for Faster File Copy

import java.io.*;

public class BufferFileCopy {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("source.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);

            FileOutputStream fos = new FileOutputStream("copy.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            int data;
            while ((data = bis.read()) != -1) {
                bos.write(data);
            }

            bis.close();
            bos.close();
            System.out.println("File copied with buffering successfully!");
        } catch (IOException e) {
            System.out.println("File operation failed: " + e);
        }
    }
}

Output:

File copied with buffering successfully!

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

  • Merging multiple log files into a single file.
    कई लॉग फाइल्स को एक ही फाइल में जोड़ना।

  • Copying large files efficiently with buffers.
    बड़े फाइल्स को तेज़ी से कॉपी करना।

  • Data backup utilities use concatenation and buffering.
    डेटा बैकअप यूटिलिटी में इसका उपयोग होता है।

Comments

Popular posts from this blog

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

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

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