Monday, August 4, 2025

๐Ÿ“š 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 operations.

เคœाเคตा เคฎें, เคฌेเคธिเค• เคฌाเค‡เคŸ เค”เคฐ เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•े เค…เคฒाเคตा, เค•เคˆ เคตिเคถेเคท เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ เคนोเคคी เคนैं เคœो เคเคกเคตांเคธ เค‡เคจเคชुเคŸ-เค†เค‰เคŸเคชुเคŸ เค‘เคชเคฐेเคถเคจ เคช्เคฐเคฆाเคจ เค•เคฐเคคी เคนैं।


Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. These classes extend InputStream, OutputStream, Reader, Writer.
    เคฏे เค•्เคฒाเคธेเคธ InputStream, OutputStream, Reader, Writer เค•ो เคเค•्เคธเคŸेंเคก เค•เคฐเคคी เคนैं।

  2. Provide special operations like file merging, compression, object serialization.
    เคฏे เคตिเคถेเคท เค‘เคชเคฐेเคถเคจ เคœैเคธे เคซाเค‡เคฒ เคฎเคฐ्เคœिंเค—, เค•เคฎ्เคช्เคฐेเคถเคจ, เค‘เคฌ्เคœेเค•्เคŸ เคธीเคฐिเคฏเคฒाเค‡เคœेเคถเคจ เค•เคฐเคคी เคนैं।

  3. Often used in real-time applications where basic streams are not enough.
    เค‡เคจเค•ा เคช्เคฐเคฏोเค— เคฐीเคฏเคฒ-เคŸाเค‡เคฎ เคเคช्เคฒिเค•ेเคถเคจ เคฎें เคนोเคคा เคนै เคœเคนाँ เคฌेเคธिเค• เคธ्เคŸ्เคฐीเคฎ เคชเคฐ्เคฏाเคช्เคค เคจเคนीं เคนोเคคी।


๐Ÿ”น Common Other Stream Classes / เคธाเคฎाเคจ्เคฏ เค…เคจ्เคฏ เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ

Stream Class Use / เค‰เคชเคฏोเค—
SequenceInputStream Combine multiple input streams
PushbackInputStream         Push data back into stream
FilterInputStream Modify input data before use
FilterOutputStream Modify output data before writing
PrintStream Print formatted data to output
ObjectInputStream Read objects from a stream
ObjectOutputStream Write objects to a stream
PipedInputStream Read data from another thread (pipe)
PipedOutputStream Send data to another thread (pipe)

๐Ÿ’ป Example 1: SequenceInputStream

Combining two files into one output file.
เคฆो เคซाเค‡เคฒ्เคธ เค•ो เคœोเคก़เค•เคฐ เคเค• เค†เค‰เคŸเคชुเคŸ เคซाเค‡เคฒ เคฎें เคฒिเค–เคจा।

import java.io.*;

public class SequenceStreamExample {
    public static void main(String[] args) throws IOException {
        FileInputStream fis1 = new FileInputStream("file1.txt");
        FileInputStream fis2 = new FileInputStream("file2.txt");
        SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

        FileOutputStream fos = new FileOutputStream("combined.txt");
        int i;
        while ((i = sis.read()) != -1) {
            fos.write(i);
        }
        sis.close();
        fos.close();
        System.out.println("Files combined successfully.");
    }
}

Sample Output:

Files combined successfully.
combined.txt → [Contents of file1 + file2]

๐Ÿ’ป Example 2: PrintStream for Formatted Output

import java.io.*;

public class PrintStreamExample {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("output.txt");
        PrintStream ps = new PrintStream(fos);

        ps.println("Hello, World!");
        ps.printf("Number: %d, Pi: %.2f", 10, 3.1415);

        ps.close();
        fos.close();
        System.out.println("Data written using PrintStream.");
    }
}

Sample Output (output.txt):

Hello, World!
Number: 10, Pi: 3.14

๐Ÿ’ป Example 3: ObjectOutputStream and ObjectInputStream

Serializing and deserializing an object.
เค‘เคฌ्เคœेเค•्เคŸ เค•ो เคธेเคต เค•เคฐเคจा เค”เคฐ เคตाเคชเคธ เคชเคข़เคจा।

import java.io.*;

class Student implements Serializable {
    int id;
    String name;
    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class ObjectStreamExample {
    public static void main(String[] args) throws Exception {
        // Writing object
        FileOutputStream fos = new FileOutputStream("student.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(new Student(101, "Aman"));
        oos.close();
        fos.close();

        // Reading object
        FileInputStream fis = new FileInputStream("student.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student s = (Student) ois.readObject();
        ois.close();
        fis.close();

        System.out.println("Student ID: " + s.id + ", Name: " + s.name);
    }
}

Sample Output:

Student ID: 101, Name: Aman

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • File Merging / Compression
    เคซाเค‡เคฒ เคฎเคฐ्เคœिंเค— / เค•เคฎ्เคช्เคฐेเคถเคจ

  • Storing and Reading Objects (Serialization)
    เค‘เคฌ्เคœेเค•्เคŸ्เคธ เค•ो เคธेเคต เค”เคฐ เคฐीเคก เค•เคฐเคจा (เคธीเคฐिเคฏเคฒाเค‡เคœेเคถเคจ)

  • Inter-thread Communication using Piped Streams
    เคฅ्เคฐेเคก्เคธ เค•े เคฌीเคš เคธंเคšाเคฐ เคชाเค‡เคช्เคก เคธ्เคŸ्เคฐीเคฎ เคฆ्เคตाเคฐा

Interactive Input and Output in Java เคœाเคตा เคฎें เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ

Interactive Input and Output in Java means taking input from the user at runtime and providing dynamic output based on the input.

เคœाเคตा เคฎें เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ เค•ा เค…เคฐ्เคฅ เคนै เคฐเคจเคŸाเค‡เคฎ เคชเคฐ เค‰เคชเคฏोเค—เค•เคฐ्เคคा เคธे เค‡เคจเคชुเคŸ เคฒेเคจा เค”เคฐ เค‰เคธเค•े เค†เคงाเคฐ เคชเคฐ เค†เค‰เคŸเคชुเคŸ เคฆेเคจा।


Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. Interactive Input: User enters data using console or GUI.
    เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ: เค‰เคชเคฏोเค—เค•เคฐ्เคคा เค•ंเคธोเคฒ เคฏा GUI เคธे เคกेเคŸा เคฆेเคคा เคนै।

  2. Interactive Output: Program responds according to input.
    เค‡ंเคŸเคฐैเค•्เคŸिเคต เค†เค‰เคŸเคชुเคŸ: เคช्เคฐोเค—्เคฐाเคฎ เค‡เคจเคชुเคŸ เค•े เค…เคจुเคธाเคฐ เค†เค‰เคŸเคชुเคŸ เคฆेเคคा เคนै।

  3. Common Classes: Scanner, BufferedReader, Console.
    เคธाเคฎाเคจ्เคฏ เค•्เคฒाเคธेเคธ: Scanner, BufferedReader, Console


๐Ÿ’ป Example 1: Using Scanner for Interactive I/O

import java.util.Scanner;

public class InteractiveScanner {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Hello " + name + "! You are " + age + " years old.");
        sc.close();
    }
}

Sample Output:

Enter your name: Rahul
Enter your age: 22
Hello Rahul! You are 22 years old.

๐Ÿ’ป Example 2: Using BufferedReader

import java.io.*;

public class InteractiveBufferedReader {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter your favorite language: ");
        String lang = br.readLine();

        System.out.println("You like " + lang + " programming!");
    }
}

Sample Output:

Enter your favorite language: Java
You like Java programming!

๐Ÿ’ป Example 3: Using Console Class

import java.io.Console;

public class InteractiveConsole {
    public static void main(String[] args) {
        Console con = System.console();
        if (con != null) {
            String user = con.readLine("Enter username: ");
            char[] pass = con.readPassword("Enter password: ");

            System.out.println("Welcome " + user + "! Your password is securely read.");
        } else {
            System.out.println("Console not available.");
        }
    }
}

Sample Output (if console available):

Enter username: admin
Enter password: *****
Welcome admin! Your password is securely read.

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • Command-line Applications: User interacts with the program.
    เค•เคฎांเคก-เคฒाเค‡เคจ เคเคช्เคฒिเค•ेเคถเคจ: เค‰เคชเคฏोเค—เค•เคฐ्เคคा เคช्เคฐोเค—्เคฐाเคฎ เคธे เคธंเคตाเคฆ เค•เคฐเคคा เคนै।

  • Login Systems: Taking username and password securely.
    เคฒॉเค—िเคจ เคธिเคธ्เคŸเคฎ: เคธुเคฐเค•्เคทिเคค เคฐूเคช เคธे เคฏूเคœ़เคฐเคจेเคฎ เค”เคฐ เคชाเคธเคตเคฐ्เคก เคฒेเคจा।

  • Interactive Games or Quizzes: Asking questions and displaying results.
    เค‡ंเคŸเคฐैเค•्เคŸिเคต เค—ेเคฎ्เคธ เคฏा เค•्เคตिเคœ़: เคช्เคฐเคถ्เคจ เคชूเค›เคจा เค”เคฐ เคชเคฐिเคฃाเคฎ เคฆिเค–ाเคจा।

๐Ÿ“„ Random Access Files in Java เคœाเคตा เคฎें เคฐैंเคกเคฎ เคเค•्เคธेเคธ เคซाเค‡เคฒ्เคธ

In Java, Random Access Files allow us to read and write data from any position in a file without reading it sequentially.

เคœाเคตा เคฎें เคฐैंเคกเคฎ เคเค•्เคธेเคธ เคซाเค‡เคฒ्เคธ เคนเคฎें เคซाเค‡เคฒ เค•े เค•िเคธी เคญी เคธ्เคฅाเคจ เคธे เคกेเคŸा เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•ी เค…เคจुเคฎเคคि เคฆेเคคी เคนैं, เคฌिเคจा เค‰เคธे เค•्เคฐเคฎเคตाเคฐ เคชเคข़े।


Key Features / เคฎुเค–्เคฏ เคตिเคถेเคทเคคाเคँ:-

  1. Direct Access: We can move to any file position using a file pointer.
    เคกाเคฏเคฐेเค•्เคŸ เคเค•्เคธेเคธ: เคนเคฎ เคซाเค‡เคฒ เคชॉเค‡ंเคŸเคฐ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคซाเค‡เคฒ เคฎें เค•िเคธी เคญी เคธ्เคฅाเคจ เคชเคฐ เคœा เคธเค•เคคे เคนैं।

  2. Read and Write Both: Same file can be used for reading and writing.
    เคฐीเคก เค”เคฐ เคฐाเค‡เคŸ เคฆोเคจों: เคเค• เคนी เคซाเค‡เคฒ เคธे เคชเคข़ เค”เคฐ เค‰เคธเคฎें เคฒिเค– เคธเค•เคคे เคนैं।

  3. Class Used: RandomAccessFile (from java.io package).
    เค‰เคชเคฏोเค— เค•ी เค—เคˆ เค•्เคฒाเคธ: RandomAccessFile (java.io เคชैเค•ेเคœ เคธे)।


๐Ÿ’ป Example 1: Writing to Random Access File

import java.io.*;

public class RandomAccessWrite {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf = new RandomAccessFile("random.txt", "rw");
            
            raf.writeUTF("Hello Random File");
            raf.writeInt(12345);
            raf.writeDouble(99.99);

            raf.close();
            System.out.println("Data written successfully!");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Output in Console:

Data written successfully!

File Content (binary, not human-readable): Stored in mixed binary format.


๐Ÿ’ป Example 2: Reading Specific Data from Random Access File

import java.io.*;

public class RandomAccessRead {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf = new RandomAccessFile("random.txt", "r");

            System.out.println("String: " + raf.readUTF());
            System.out.println("Integer: " + raf.readInt());
            System.out.println("Double: " + raf.readDouble());

            raf.close();
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Output:

String: Hello Random File
Integer: 12345
Double: 99.99

๐Ÿ’ป Example 3: Jump to a Specific Position

import java.io.*;

public class RandomAccessSeek {
    public static void main(String[] args) {
        try {
            RandomAccessFile raf = new RandomAccessFile("random.txt", "rw");

            raf.seek(0);  // Move pointer to start
            System.out.println("First UTF: " + raf.readUTF());

            raf.seek(raf.length()); // Move to end and append
            raf.writeUTF(" - Appended Text");

            raf.close();
            System.out.println("Data appended successfully!");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Output:

First UTF: Hello Random File
Data appended successfully!

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • Database Systems: Direct record access without reading full file.
    เคกेเคŸाเคฌेเคธ เคธिเคธ्เคŸเคฎ: เคชूเคฐी เคซाเค‡เคฒ เคชเคข़े เคฌिเคจा เคฐिเค•ॉเคฐ्เคก เคคเค• เคชเคนुँเคšเคจा।

  • Media Players: Jumping to a specific timestamp in audio/video files.
    เคฎीเคกिเคฏा เคช्เคฒेเคฏเคฐ: เค•िเคธी เคตिเคถेเคท เคธเคฎเคฏ เคชเคฐ เคœाเคจे เค•े เคฒिเค।

  • File Editors: Modify files without rewriting them completely.
    เคซाเค‡เคฒ เคเคกिเคŸเคฐ: เคชूเคฐी เคซाเค‡เคฒ เคซिเคฐ เคธे เคฒिเค–े เคฌिเคจा เคธंเคถोเคงเคจ เค•เคฐเคจा।

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.
    เคกेเคŸा เคฌैเค•เค…เคช เคฏूเคŸिเคฒिเคŸी เคฎें เค‡เคธเค•ा เค‰เคชเคฏोเค— เคนोเคคा เคนै।

Handling Primitive Data Types in Java I/O เคœाเคตा I/O เคฎें เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เค•ो เคธंเคญाเคฒเคจा

Java provides special Data Streams to read and write primitive data types like int, float, boolean, and char.

เคœाเคตा เคกाเคŸा เคธ्เคŸ्เคฐीเคฎ्เคธ เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै เคœो เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคœैเคธे int, float, boolean, เค”เคฐ char เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค‰เคชเคฏोเค— เคนोเคคी เคนैं।


Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. DataOutputStream writes primitive data types to a file.
    DataOutputStream เคซाเค‡เคฒ เคฎें เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคฒिเค–เคคा เคนै।

  2. DataInputStream reads primitive data types from a file.
    DataInputStream เคซाเค‡เคฒ เคธे เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคชเคข़เคคा เคนै।

  3. These are useful for structured data storage.
    เคฏเคน เคธ्เคŸ्เคฐเค•्เคšเคฐ्เคก เคกेเคŸा เคธ्เคŸोเคฐेเคœ เค•े เคฒिเค เค‰เคชเคฏोเค—ी เคนैं।


๐Ÿ’ป Example 1: Writing Primitive Data Types

import java.io.*;

public class WritePrimitives {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("data.bin");
            DataOutputStream dos = new DataOutputStream(fos);

            dos.writeInt(101);
            dos.writeUTF("Ajay");
            dos.writeFloat(95.5f);
            dos.writeBoolean(true);

            dos.close();
            fos.close();
            System.out.println("Primitive data written successfully.");
        } catch (IOException e) {
            System.out.println("Error writing data.");
        }
    }
}

Output:

Primitive data written successfully.

๐Ÿ’ป Example 2: Reading Primitive Data Types

import java.io.*;

public class ReadPrimitives {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("data.bin");
            DataInputStream dis = new DataInputStream(fis);

            int id = dis.readInt();
            String name = dis.readUTF();
            float marks = dis.readFloat();
            boolean status = dis.readBoolean();

            dis.close();
            fis.close();

            System.out.println("ID: " + id);
            System.out.println("Name: " + name);
            System.out.println("Marks: " + marks);
            System.out.println("Status: " + status);
        } catch (IOException e) {
            System.out.println("Error reading data.");
        }
    }
}

Output:

ID: 101
Name: Ajay
Marks: 95.5
Status: true

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • Storing structured data like employee records
    เคธ्เคŸ्เคฐเค•्เคšเคฐ्เคก เคกेเคŸा เคœैเคธे เค•เคฐ्เคฎเคšाเคฐी เคฐिเค•ॉเคฐ्เคก เคธ्เคŸोเคฐ เค•เคฐเคจा

  • Game development for saving game states and scores
    เค—ेเคฎ เคกेเคตเคฒเคชเคฎेंเคŸ เคฎें เค—ेเคฎ เคธ्เคŸेเคŸ เค”เคฐ เคธ्เค•ोเคฐ เคธेเคต เค•เคฐเคจा

  • Reading sensor data in IoT applications
    IoT เคเคช्เคฒिเค•ेเคถเคจ เคฎें เคธेंเคธเคฐ เคกेเคŸा เคชเคข़เคจा

Reading and Writing Bytes in Java / เคœाเคตा เคฎें เคฌाเค‡เคŸ्เคธ เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा

Java provides Byte Streams to handle 8-bit data (bytes) for reading and writing files.

เคœाเคตा เคฌाเค‡เคŸ เคธ्เคŸ्เคฐीเคฎ्เคธ เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै, เคœो 8-เคฌिเคŸ เคกेเคŸा (เคฌाเค‡เคŸ्เคธ) เค•ो เคซाเค‡เคฒ เคธे เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เคช्เคฐเคฏोเค— เคนोเคคी เคนैं।

Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. Use FileInputStream for reading bytes from a file.
    เคซाเค‡เคฒ เคธे เคฌाเค‡เคŸ्เคธ เคชเคข़เคจे เค•े เคฒिเค FileInputStream เค•ा เค‰เคชเคฏोเค— เค•เคฐें।

  2. Use FileOutputStream for writing bytes to a file.
    เคซाเค‡เคฒ เคฎें เคฌाเค‡เคŸ्เคธ เคฒिเค–เคจे เค•े เคฒिเค FileOutputStream เค•ा เค‰เคชเคฏोเค— เค•เคฐें।

  3. Suitable for binary files like images, PDFs, and videos.
    เคฏเคน เคฌाเค‡เคจเคฐी เคซाเค‡เคฒ्เคธ เคœैเคธे เค‡เคฎेเคœ, PDF เค”เคฐ เคตीเคกिเคฏो เค•े เคฒिเค เค‰เคชเคฏुเค•्เคค เคนै।


๐Ÿ’ป Example 1: Writing Bytes to a File

import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBytesExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("bytefile.txt");
            String data = "Hello Byte Stream!";
            fos.write(data.getBytes());
            fos.close();
            System.out.println("Bytes written successfully.");
        } catch (IOException e) {
            System.out.println("Error writing file.");
        }
    }
}

Output:

Bytes written successfully.

bytefile.txt content:

Hello Byte Stream!

๐Ÿ’ป Example 2: Reading Bytes from a File

import java.io.FileInputStream;
import java.io.IOException;

public class ReadBytesExample {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("bytefile.txt");
            int i;
            while ((i = fis.read()) != -1) {
                System.out.print((char) i);
            }
            fis.close();
        } catch (IOException e) {
            System.out.println("Error reading file.");
        }
    }
}

Output:

Hello Byte Stream!

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • Reading and writing binary files like images, audio, or video
    เค‡เคฎेเคœ, เค‘เคกिเคฏो เคฏा เคตीเคกिเคฏो เคœैเคธी เคฌाเค‡เคจเคฐी เคซाเค‡เคฒ्เคธ เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เคฎें

  • File transfer operations
    เคซाเค‡เคฒ เคŸ्เคฐांเคธเคซเคฐ เค‘เคชเคฐेเคถเคจ्เคธ

  • Handling large files in raw byte format
    เคฐॉ เคฌाเค‡เคŸ เคซॉเคฐ्เคฎेเคŸ เคฎें เคฌเคก़ी เคซाเค‡เคฒ्เคธ เคนैंเคกเคฒ เค•เคฐเคจा

Reading and Writing Characters in Java เคœाเคตा เคฎें เค•เคฐैเค•्เคŸเคฐ เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा

Reading and writing characters in Java is done using Character Streams, which handle 16-bit Unicode characters.

เคœाเคตा เคฎें เค•เคฐैเค•्เคŸเคฐ เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœाเคคा เคนै, เคœो 16-เคฌिเคŸ เคฏूเคจिเค•ोเคก เค•เคฐैเค•्เคŸเคฐ เค•ो เคธंเคญाเคฒเคคे เคนैं।

Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. Use FileReader for reading characters from a file.
    เคซाเค‡เคฒ เคธे เค•เคฐैเค•्เคŸเคฐ เคชเคข़เคจे เค•े เคฒिเค FileReader เค•ा เค‰เคชเคฏोเค— เค•เคฐें।

  2. Use FileWriter for writing characters to a file.
    เคซाเค‡เคฒ เคฎें เค•เคฐैเค•्เคŸเคฐ เคฒिเค–เคจे เค•े เคฒिเค FileWriter เค•ा เค‰เคชเคฏोเค— เค•เคฐें।

  3. Wrap them in BufferedReader and BufferedWriter for efficiency.
    เคฆเค•्เคทเคคा เคฌเคข़ाเคจे เค•े เคฒिเค BufferedReader เค”เคฐ BufferedWriter เค•ा เค‰เคชเคฏोเค— เค•เคฐें।


๐Ÿ’ป Example 1: Writing Characters to a File

import java.io.FileWriter;
import java.io.IOException;

public class WriteCharactersExample {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("charfile.txt");
            writer.write("Hello, Character Stream!");
            writer.close();
            System.out.println("Characters written successfully.");
        } catch (IOException e) {
            System.out.println("Error writing file.");
        }
    }
}

Output:

Characters written successfully.

charfile.txt content:

Hello, Character Stream!

๐Ÿ’ป Example 2: Reading Characters from a File

import java.io.FileReader;
import java.io.IOException;

public class ReadCharactersExample {
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader("charfile.txt");
            int i;
            while ((i = reader.read()) != -1) {
                System.out.print((char) i);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("Error reading file.");
        }
    }
}

Output:

Hello, Character Stream!

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • Reading configuration files or notes in plain text
    เค•ॉเคจ्เคซ़िเค—เคฐेเคถเคจ เคซाเค‡เคฒ्เคธ เคฏा เคจोเคŸ्เคธ เค•ो เคชเคข़เคจे เคฎें

  • Writing logs or reports in text format
    เคŸेเค•्เคธ्เคŸ เคซॉเคฐ्เคฎेเคŸ เคฎें เคฒॉเค— เคฏा เคฐिเคชोเคฐ्เคŸ เคฒिเค–เคจे เคฎें

  • Simple data storage for small applications
    เค›ोเคŸी เคเคช्เคฒीเค•ेเคถเคจ เค•े เคฒिเค เคธเคฐเคฒ เคกेเคŸा เคธ्เคŸोเคฐेเคœ

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

  เคฌैเค•เค…เคช เคฏा เค•ैเคถिंเค— เค•े เคฒिเค เค…เคธ्เคฅाเคฏी เคซाเค‡เคฒ เคจिเคฐ्เคฎाเคฃ

Input/Output Exceptions in Java เคœाเคตा เคฎें เค‡เคจเคชुเคŸ/เค†เค‰เคŸเคชुเคŸ เคเค•्เคธेเคช्เคถเคจ्เคธ

Input / Output (I/O) exceptions occur when a program fails to read or write data from a file, device, or network. These exceptions are part of the java.io package and usually occur due to:

เค‡เคจเคชुเคŸ / เค†เค‰เคŸเคชुเคŸ (I/O) เคเค•्เคธेเคช्เคถเคจ เคคเคฌ เคนोเคคी เคนै เคœเคฌ เคช्เคฐोเค—्เคฐाเคฎ เคซाเค‡เคฒ, เคกिเคตाเค‡เคธ เคฏा เคจेเคŸเคตเคฐ्เค• เคธे เคกेเคŸा เคชเคข़เคจे เคฏा เคฒिเค–เคจे เคฎें เค…เคธเคซเคฒ เคฐเคนเคคा เคนै। เคฏे เคเค•्เคธेเคช्เคถเคจ java.io เคชैเค•ेเคœ เค•ा เคนिเคธ्เคธा เคนोเคคी เคนैं เค”เคฐ เคฎुเค–्เคฏ เคฐूเคช เคธे เค‡เคจ เค•ाเคฐเคฃों เคธे เคนोเคคी เคนैं:

  • File not found เคซाเค‡เคฒ เค•ा เคจ เคฎिเคฒเคจा

  • Lack of read/write permissions เคชเคข़เคจे/เคฒिเค–เคจे เค•ी เค…เคจुเคฎเคคि เคจ เคนोเคจा

  • Disk or device failure เคกिเคธ्เค• เคฏा เคกिเคตाเค‡เคธ เคซेเคฒ เคนोเคจा

  • Network disconnection during I/O operations เคจेเคŸเคตเคฐ्เค• เค•เคจेเค•्เคถเคจ เคŸूเคŸ เคœाเคจा

In Java some common I/O exceptions are as follows เคœाเคตा เคฎें เค•ुเค› เคธाเคฎाเคจ्เคฏ I/O เคเค•्เคธेเคช्เคถเคจ เคจिเคฎ्เคจ เคนै:-

  • IOException – General exception for I/O errors เคธाเคฎाเคจ्เคฏ I/O เคเคฐเคฐ

  • FileNotFoundException – File is missing or inaccessible เคซाเค‡เคฒ เค—ाเคฏเคฌ เคฏा เคเค•्เคธेเคธ เคจ เคนो เคฐเคนी เคนो

  • EOFException – End of file reached unexpectedly เคซाเค‡เคฒ เค…เคšाเคจเค• เคธเคฎाเคช्เคค เคนो เค—เคˆ

We can handle these exceptions using try-catch blocks to prevent program crashes.

เค‡เคจ เคเค•्เคธेเคช्เคถเคจ เค•ो try-catch เคฌ्เคฒॉเค• เคธे เคนैंเคกเคฒ เค•िเคฏा เคœाเคคा เคนै เคคाเค•ि เคช्เคฐोเค—्เคฐाเคฎ เค•्เคฐैเคถ เคจ เคนो।


1️⃣ IOException Example

IOException is the base class for all input/output exceptions in Java.

import java.io.FileReader;
import java.io.IOException;

public class IOExceptionDemo {
    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("sample.txt"); // If file missing, IOException occurs
            int ch;
            while ((ch = fr.read()) != -1) {
                System.out.print((char) ch);
            }
            fr.close();
        } catch (IOException e) {
            System.out.println("IOException Occurred: " + e.getMessage());
        }
    }
}

Output (if file is missing):

IOException Occurred: sample.txt (The system cannot find the file specified)

2️⃣ FileNotFoundException Example

This is a subclass of IOException and occurs when the file is not found.

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FileNotFoundDemo {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("nofile.txt"); // File does not exist
            System.out.println("File opened successfully.");
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException Occurred: " + e.getMessage());
        }
    }
}

Output:

FileNotFoundException Occurred: nofile.txt (The system cannot find the file specified)

3️⃣ EOFException Example

EOFException (End of File) occurs when input operations reach the end of a file unexpectedly, usually with DataInputStream.

import java.io.*;

public class EOFExceptionDemo {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("data.bin");
            DataOutputStream dos = new DataOutputStream(fos);
            dos.writeInt(100); // writing single integer
            dos.close();
            
            FileInputStream fis = new FileInputStream("data.bin");
            DataInputStream dis = new DataInputStream(fis);
            
            System.out.println("First Number: " + dis.readInt());
            System.out.println("Second Number: " + dis.readInt()); // No second number → EOFException
            dis.close();
        } catch (EOFException e) {
            System.out.println("EOFException Occurred: End of file reached.");
        } catch (IOException e) {
            System.out.println("IOException Occurred: " + e.getMessage());
        }
    }
}

Output:

First Number: 100
EOFException Occurred: End of file reached.

⚡ Real-life Use

  • เคธुเคฐเค•्เคทिเคค เคฐूเคช เคธे เคซाเค‡เคฒ เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा

  • เคจेเคŸเคตเคฐ्เค• เค†เคงाเคฐिเคค เคเคช्เคฒीเค•ेเคถเคจ เคฎें เคเคฐเคฐ เคนैंเคกเคฒिंเค—

  • เคซाเค‡เคฒ เคฏा เคกिเคตाเค‡เคธ เคซेเคฒ เคนोเคจे เคชเคฐ เคช्เคฐोเค—्เคฐाเคฎ เค•ो เค•्เคฐैเคถ เคนोเคจे เคธे เคฌเคšाเคจा

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...