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.
    เคซाเค‡เคฒ เค•ी เคช्เคฐॉเคชเคฐ्เคŸीเคœ เคœैเคธे เคธाเค‡เคœ़, เคจाเคฎ เคฏा เคชเคฐเคฎिเคถเคจ เค•ो เคชเคข़เคจा

Character Stream in Java เคœाเคตा เคฎें เค•เคฐैเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ

Character Stream in Java is used to read and write character data (16-bit Unicode) instead of raw bytes.

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

Character Streams are mainly used for text files because they handle Unicode automatically.
เค•เคฐैเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เคช्เคฐเคฏोเค— เคช्เคฐाเคฏः เคŸेเค•्เคธ्เคŸ เคซाเค‡เคฒ्เคธ เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै เค•्เคฏोंเค•ि เคฏे เคฏूเคจिเค•ोเคก เค•ो เคธ्เคตเคคः เคนैंเคกเคฒ เค•เคฐเคคी เคนैं।


1️⃣ Important Character Stream Classes

Class Name Description (English) เคตिเคตเคฐเคฃ (Hindi)
FileReader Reads data from file เคซाเค‡เคฒ เคธे เคกेเคŸा เคชเคข़เคคा เคนै
FileWriter Writes data to file เคซाเค‡เคฒ เคฎें เคกेเคŸा เคฒिเค–เคคा เคนै
BufferedReader Reads data using buffer เคฌเคซเคฐ เค•े เคธाเคฅ เค•เคฐैเค•्เคŸเคฐ เคชเคข़เคคा เคนै
BufferedWriter Writes data using buffer เคฌเคซเคฐ เค•े เคธाเคฅ เค•เคฐैเค•्เคŸเคฐ เคฒिเค–เคคा เคนै
PrintWriter Writes formatted text output เคช्เคฐिंเคŸ เค”เคฐ เคช्เคฐिंเคŸเคฒเคจ เคฎेเคฅเคก्เคธ เค•े เคธाเคฅ เคฒिเค–เคคा เคนै
InputStreamReader Converts bytes to characters เคฌाเค‡เคŸ เค•ो เค•เคฐैเค•्เคŸเคฐ เคฎें เคฌเคฆเคฒเคคा เคนै
OutputStreamWriter Converts characters to bytes เค•เคฐैเค•्เคŸเคฐ เค•ो เคฌाเค‡เคŸ เคฎें เคฌเคฆเคฒเคคा เคนै
Reader Abstract class for character input เค•เคฐैเค•्เคŸเคฐ เค‡เคจเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เคธुเคชเคฐเค•्เคฒाเคธ
Writer Abstract class for character output เค•เคฐैเค•्เคŸเคฐ เค†เค‰เคŸเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เคธुเคชเคฐเค•्เคฒाเคธ

2️⃣ Character Stream Examples

๐Ÿ“ Example 1: Read File using BufferedReader

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileReader fr = new FileReader("file.txt");
            BufferedReader br = new BufferedReader(fr);
            int i;
            while((i = br.read()) != -1) {
                System.out.print((char)i);
            }
            br.close();
            fr.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

๐Ÿ“ Example 2: Write File using BufferedWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("Hello World!");
            bw.close();
            fw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Data written successfully.
file.txt → Hello World!

๐Ÿ“ Example 3: Write File using PrintWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            PrintWriter pw = new PrintWriter(fw);
            pw.write("Hello Friends!");
            pw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Data written successfully.
file.txt → Hello Friends!

๐Ÿ“ Example 4: Simple File Write using FileWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            fw.write("Hello World!");
            fw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

๐Ÿ“ Example 5: Simple File Read using FileReader

import java.io.*;

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

Output:

Hello World!

Byte Stream in Java เคœाเคตा เคฎें เคฌाเค‡เคŸ เคธ्เคŸ्เคฐीเคฎ

Byte Stream in Java is used to read and write 8-bit binary data like files, images, audio, and video.

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

  1. InputStream (เค‡เคจเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ)

  2. OutputStream (เค†เค‰เคŸเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ)


1️⃣ InputStream (เค‡เคจเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ)

InputStream is a superclass that reads data from a source like file, device, or socket.
InputStream เคเค• เคธुเคชเคฐเค•्เคฒाเคธ เคนै เคœो เคกेเคŸा เค•ो เค•िเคธी เคธ्เคค्เคฐोเคค (เคซाเค‡เคฒ, เคกिเคตाเค‡เคธ, เคฏा เคธॉเค•ेเคŸ) เคธे เคชเคข़เคคी เคนै।

๐Ÿ”น Common Methods of InputStream

Method Description (English) เคตिเคตเคฐเคฃ (Hindi)
int read()          Reads next byte of data                       เค…เค—เคฒी เคฌाเค‡เคŸ เค•ो เคชเคข़เคคा เคนै
int available() Returns number of available bytes เค‰เคชเคฒเคฌ्เคง เคฌाเค‡เคŸ्เคธ เค•ी เคธंเค–्เคฏा เคฒौเคŸाเคคा เคนै
void close() Closes the stream เคธ्เคŸ्เคฐीเคฎ เค•ो เคฌंเคฆ เค•เคฐเคคा เคนै

2️⃣ OutputStream (เค†เค‰เคŸเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ)

OutputStream is a superclass that writes data to a destination like file, device, or socket.
OutputStream เคเค• เคธुเคชเคฐเค•्เคฒाเคธ เคนै เคœो เคกेเคŸा เค•ो เค•िเคธी เค—ंเคคเคต्เคฏ (เคซाเค‡เคฒ, เคกिเคตाเค‡เคธ, เคฏा เคธॉเค•ेเคŸ) เคฎें เคฒिเค–เคคी เคนै।

๐Ÿ”น Common Methods of OutputStream

Method      Description (English) เคตिเคตเคฐเคฃ (Hindi)
void write(int)           Writes a byte                       เคเค• เคฌाเค‡เคŸ เคฒिเค–เคคा เคนै
void write(byte[]) Writes byte array เคฌाเค‡เคŸ เคเคฐे เคฒिเค–เคคा เคนै
void flush() Flushes the stream เคธ्เคŸ्เคฐीเคฎ เค•ो เคฐिเคซ्เคฐेเคถ เค•เคฐเคคा เคนै
void close() Closes the stream เคธ्เคŸ्เคฐीเคฎ เค•ो เคฌंเคฆ เค•เคฐเคคा เคนै

3️⃣ Important Byte Stream Classes

Class Name Description (English) เคตिเคตเคฐเคฃ (Hindi)
FileInputStream Reads byte data from file เคซाเค‡เคฒ เคธे เคฌाเค‡เคŸ เคกेเคŸा เคชเคข़เคคा เคนै
FileOutputStream Writes byte data to file เคซाเค‡เคฒ เคฎें เคฌाเค‡เคŸ เคกेเคŸा เคฒिเค–เคคा เคนै
BufferedInputStream Reads data using buffer เคฌเคซเคฐ เค•े เคธाเคฅ เคชเคข़เคคा เคนै
BufferedOutputStream Writes data using buffer เคฌเคซเคฐ เค•े เคธाเคฅ เคฒिเค–เคคा เคนै
DataInputStream Reads primitive data types เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคชเคข़เคคा เคนै
DataOutputStream Writes primitive data types เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคฒिเค–เคคा เคนै
PrintStream Writes formatted output เคซॉเคฐ्เคฎेเคŸेเคก เค†เค‰เคŸเคชुเคŸ เคฒिเค–เคคा เคนै

4️⃣ Byte Stream Examples

๐Ÿ“ Example 1: Read File using BufferedInputStream

import java.io.*;

public class Sample {
    public static void main(String args[]) {
        try {
            FileInputStream fis = new FileInputStream("file.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);
            int i;
            while((i = bis.read()) != -1) {
                System.out.print((char)i);
            }
            bis.close();
            fis.close();
        } catch(FileNotFoundException e1) {
            System.out.println("File not found");
        } catch(Exception e2) {
            System.out.println("Reading file error");
        }
    }
}

Output:

Example for BufferedInputStream.

๐Ÿ“ Example 2: Write File using BufferedOutputStream

import java.io.*;

public class Sample {
    public static void main(String args[]) {
        try {
            FileOutputStream fos = new FileOutputStream("file1.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            String str = "Example for BufferedOutputStream.";
            bos.write(str.getBytes());
            bos.close();
            fos.close();
            System.out.println("File is created successfully");
        } catch(Exception e) {
            System.out.println("File is not created");
        }
    }
}

๐Ÿ“ Example 3: Write Primitive Data using DataOutputStream

import java.io.*;

public class Sample {
    public static void main(String args[]) {
        int id = 1;
        String name = "Ajay Tiwari";
        float salary = 25000.50f;
        try {
            FileOutputStream fos = new FileOutputStream("file.txt");
            DataOutputStream dos = new DataOutputStream(fos);
            dos.writeInt(id);
            dos.writeUTF(name);
            dos.writeFloat(salary);
            dos.close();
            System.out.println("Data written successfully.");
        } catch(IOException e) {
            System.out.println(e);
        }
    }
}

๐Ÿ“ Example 4: Read Primitive Data using DataInputStream

import java.io.*;

class Sample1 {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("file.txt");
            DataInputStream dis = new DataInputStream(fis);
            System.out.println("ID : " + dis.readInt());
            System.out.println("Name : " + dis.readUTF());
            System.out.println("Salary : " + dis.readFloat());
        } catch(IOException e) {
            System.out.println(e);
        }
    }
}

Output:

ID : 1
Name : Ajay Tiwari
Salary : 25000.50

๐Ÿ“ Example 5: Simple File Write using FileOutputStream

import java.io.*;

public class Sample {
    public static void main(String args[]) {
        try {
            FileOutputStream fos = new FileOutputStream("file.txt");
            String str = "Hello World!";
            fos.write(str.getBytes());
            fos.close();
            System.out.println("File written successfully");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

๐Ÿ“ Example 6: Simple File Read using FileInputStream

import java.io.*;

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

๐Ÿ“ Example 7: Write File using PrintStream

import java.io.*;

public class Sample {
    public static void main(String args[]) {
        try {
            FileOutputStream fos = new FileOutputStream("file.txt");
            PrintStream ps = new PrintStream(fos);
            ps.println("Hello World!");
            ps.close();
            fos.close();
            System.out.println("Data Written Successfully");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

เคœाเคตा เคฎें เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค— (Java File Handling in Hindi & English)

File handling in Java allows us to read and write data permanently in files. Using Java I/O (Input/Output) streams, we can store data, read it, and even process it efficiently.

เคœाเคตा เคฎें เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค— เคนเคฎें เคกेเคŸा เค•ो เคธ्เคฅाเคฏी เคฐूเคช เคธे เคธेเคต เค”เคฐ เคชเคข़เคจे เค•ी เคธुเคตिเคงा เคฆेเคคी เคนै। เคœाเคตा I/O (เค‡เคจเคชुเคŸ/เค†เค‰เคŸเคชुเคŸ) เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ी เคฎเคฆเคฆ เคธे เคนเคฎ เคกेเคŸा เค•ो เคธ्เคŸोเคฐ, เคชเคข़ เค”เคฐ เคช्เคฐोเคธेเคธ เค•เคฐ เคธเค•เคคे เคนैं।


1️⃣ เคœाเคตा เคฎें เคธ्เคŸ्เคฐीเคฎ (Java Streams)

stream is a sequence of data. In Java, streams are used to read and write data from one location to another (like file, memory, or network).
เคธ्เคŸ्เคฐीเคฎ เคกेเคŸा เค•ा เคเค• เค•्เคฐเคฎ เคนोเคคा เคนै। เคœाเคตा เคฎें, เคธ्เคŸ्เคฐीเคฎ เค•ा เค‰เคชเคฏोเค— เคกेเคŸा เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै, เคœैเคธे เคซ़ाเค‡เคฒ, เคฎेเคฎोเคฐी เคฏा เคจेเคŸเคตเคฐ्เค•।

Java I/O streams are mainly divided into two types:
เคœाเคตा I/O เคธ्เคŸ्เคฐीเคฎ เคฎुเค–्เคฏเคคः เคฆो เคช्เคฐเค•ाเคฐ เค•ी เคนोเคคी เคนैं:

1️⃣ Character Stream – เค•ैเคฐेเค•्เคŸเคฐ (2 เคฌाเค‡เคŸ เคฏूเคจिเค•ोเคก) เคกेเคŸा เค•े เคฒिเค

  • Character Stream → Reader เค”เคฐ Writer เค•्เคฒाเคธेเคธ


2️⃣ Byte Stream – เคฌाเค‡เคŸ (8-เคฌिเคŸ) เคกेเคŸा เค•े เคฒिเค

  • Byte Stream → InputStream เค”เคฐ OutputStream เค•्เคฒाเคธेเคธ

  • Buffering เค•े เคฒिเค → BufferedReaderBufferedWriterBufferedInputStreamBufferedOutputStream

  • Printing เค”เคฐ Formatting เค•े เคฒिเค → PrintWriterPrintStream

  • เคซ़ाเค‡เคฒ เคฎें เคกेเคŸा เค•ो เคธ्เคฅाเคฏी เคฐूเคช เคธे เคธेเคต เค•เคฐ เคธเค•เคคे เคนैं। เคฌเคก़ी เคซाเค‡เคฒ्เคธ เค•ो เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा เค†เคธाเคจ। เคธ्เคŸ्เคฐीเคฎ्เคธ เค”เคฐ เคฌเคซเคฐिंเค— เคธे เคชเคฐเคซॉเคฐ्เคฎेंเคธ เคฌेเคนเคคเคฐ। เคจेเคŸเคตเคฐ्เค•िंเค— เค”เคฐ เคกेเคŸाเคฌेเคธ เค‘เคชเคฐेเคถเคจ เคฎें เคกाเคŸा เคธ्เคŸ्เคฐीเคฎिंเค— เค†เคธाเคจ।


2️⃣ Character Stream (เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ)

Character streams are used to read and write character data.
เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ เค•ा เคช्เคฐเคฏोเค— เค•ैเคฐेเค•्เคŸเคฐ เคกेเคŸा เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

✨ Common Character Stream Classes

Class NameDescription (English)เคตिเคตเคฐเคฃ (Hindi)
FileReaderReads data from a file (character by character)เคซाเค‡เคฒ เคธे เค•ैเคฐेเค•्เคŸเคฐ เคกेเคŸा เคชเคข़เคคा เคนै
FileWriterWrites data to a fileเคซाเค‡เคฒ เคฎें เค•ैเคฐेเค•्เคŸเคฐ เคกेเคŸा เคฒिเค–เคคा เคนै
BufferedReaderReads text from an input stream efficientlyเคฌเคซเคฐ เค•े เคธाเคฅ เค•ैเคฐेเค•्เคŸเคฐ เคชเคข़เคคा เคนै
BufferedWriterWrites text to an output stream efficientlyเคฌเคซเคฐ เค•े เคธाเคฅ เค•ैเคฐेเค•्เคŸเคฐ เคฒिเค–เคคा เคนै
PrintWriterAllows printing formatted textเคซॉเคฐ्เคฎेเคŸेเคก เคŸेเค•्เคธ्เคŸ เคช्เคฐिंเคŸ เค•เคฐเคจे เค•ी เคธुเคตिเคงा

๐Ÿ“ Example 1: Reading File using BufferedReader

import java.io.*;

class ReadFileExample {
    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("file.txt");
            BufferedReader br = new BufferedReader(fr);
            String line;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            fr.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

๐Ÿ“ Example 2: Writing File using FileWriter

import java.io.*;

class WriteFileExample {
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            fw.write("Hello World!");
            fw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

3️⃣ Byte Stream (เคฌाเค‡เคŸ เคธ्เคŸ्เคฐीเคฎ)

Byte streams are used to handle binary data like images, videos, or audio files.
เคฌाเค‡เคŸ เคธ्เคŸ्เคฐीเคฎ เค•ा เคช्เคฐเคฏोเค— เคฌाเค‡เคจเคฐी เคกेเคŸा เคœैเคธे เค‡เคฎेเคœ, เคตीเคกिเคฏो เคฏा เค‘เคกिเคฏो เค•ो เคชเคข़เคจे-เคฒिเค–เคจे เคฎें เค•िเคฏा เคœाเคคा เคนै।

✨ Common Byte Stream Classes

Class NameDescription (English)เคตिเคตเคฐเคฃ (Hindi)
FileInputStreamReads bytes from a fileเคซाเค‡เคฒ เคธे เคฌाเค‡เคŸ เคชเคข़เคคा เคนै
FileOutputStreamWrites bytes to a fileเคซाเค‡เคฒ เคฎें เคฌाเค‡เคŸ เคฒिเค–เคคा เคนै
BufferedInputStreamReads data with bufferingเคฌเคซเคฐ เค•े เคธाเคฅ เคฌाเค‡เคŸ เคชเคข़เคคा เคนै
BufferedOutputStreamWrites data with bufferingเคฌเคซเคฐ เค•े เคธाเคฅ เคฌाเค‡เคŸ เคฒिเค–เคคा เคนै
DataInputStreamReads primitive data typesเคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคชเคข़เคคा เคนै
DataOutputStreamWrites primitive data typesเคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคฒिเค–เคคा เคนै
PrintStreamPrints formatted outputเคซॉเคฐ्เคฎेเคŸेเคก เค†เค‰เคŸเคชुเคŸ เคฒिเค–เคคा เคนै

๐Ÿ“ Example 3: Reading File using FileInputStream

import java.io.*;

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

๐Ÿ“ Example 4: Writing File using FileOutputStream

import java.io.*;

class FileOutputExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("file.txt");
            String str = "Hello Java!";
            fos.write(str.getBytes());
            fos.close();
            System.out.println("File written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

File written successfully.

4️⃣ เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค— เค•े เคซाเคฏเคฆे (Advantages)

  • Permanent data storage (เคธ्เคฅाเคฏी เคธ्เคŸोเคฐेเคœ)

  • Easy reading/writing of large files (เคฌเคก़ी เคซ़ाเค‡เคฒ เคชเคข़เคจा/เคฒिเค–เคจा เค†เคธाเคจ)

  • Supports both text and binary data

  • Useful in database, networking and real-time applications

เคœाเคตा เคฎें เคธ्เคŸ्เคฐीเคฎ Java Streams in File Handling

เคธ्เคŸ्เคฐीเคฎ เคกेเคŸा เค•ो เคเค• เคœเค—เคน เคธे เคฆूเคธเคฐी เคœเค—เคน เคŸ्เคฐांเคธเคซเคฐ เค•เคฐเคจे เค•ा เคฎाเคง्เคฏเคฎ เคนोเคคी เคนै। เคœाเคตा เคฎें เคธ्เคŸ्เคฐीเคฎ เคฆो เคช्เคฐเค•ाเคฐ เค•ी เคนोเคคी เคนै:

1️⃣ Character Stream – เค•ैเคฐेเค•्เคŸเคฐ (2 เคฌाเค‡เคŸ เคฏूเคจिเค•ोเคก) เคกेเคŸा เค•े เคฒिเค

  • Character Stream → Reader เค”เคฐ Writer เค•्เคฒाเคธेเคธ


2️⃣ Byte Stream – เคฌाเค‡เคŸ (8-เคฌिเคŸ) เคกेเคŸा เค•े เคฒिเค

  • Byte Stream → InputStream เค”เคฐ OutputStream เค•्เคฒाเคธेเคธ

  • Buffering เค•े เคฒिเค → BufferedReader, BufferedWriter, BufferedInputStream, BufferedOutputStream

  • Printing เค”เคฐ Formatting เค•े เคฒिเค → PrintWriter, PrintStream

  • เคซ़ाเค‡เคฒ เคฎें เคกेเคŸा เค•ो เคธ्เคฅाเคฏी เคฐूเคช เคธे เคธेเคต เค•เคฐ เคธเค•เคคे เคนैं। เคฌเคก़ी เคซाเค‡เคฒ्เคธ เค•ो เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा เค†เคธाเคจ। เคธ्เคŸ्เคฐीเคฎ्เคธ เค”เคฐ เคฌเคซเคฐिंเค— เคธे เคชเคฐเคซॉเคฐ्เคฎेंเคธ เคฌेเคนเคคเคฐ। เคจेเคŸเคตเคฐ्เค•िंเค— เค”เคฐ เคกेเคŸाเคฌेเคธ เค‘เคชเคฐेเคถเคจ เคฎें เคกाเคŸा เคธ्เคŸ्เคฐीเคฎिंเค— เค†เคธाเคจ।

Java Input & Output Streams เคœाเคตा เค‡เคจเคชुเคŸ เคเคตं เค†เค‰เคŸเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ

In every programming language, there is a mechanism to handle input and output. In Java, input and output operations are mainly performed using streams, which are part of the java.io package.

เคนเคฐ เคช्เคฐोเค—्เคฐाเคฎिंเค— เคฒैंเค—्เคตेเคœ เคฎें เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ เค•ो เคธंเคญाเคฒเคจे เค•ी เคต्เคฏเคตเคธ्เคฅा เคนोเคคी เคนै। เคœाเคตा เคฎें เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ เค•ाเคฐ्เคฏ เคฎुเค–्เคฏ เคฐूเคช เคธे เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เค•िเค เคœाเคคे เคนैं, เคœो java.io เคชैเค•ेเคœ เค•ा เคนिเคธ्เคธा เคนैं।


Basic Console I/O Streams in Java เคœाเคตा เคฎें เคฌेเคธिเค• เค•ंเคธोเคฒ I/O เคธ्เคŸ्เคฐीเคฎ्เคธ

Java provides three standard I/O streams for console input and output:
เคœाเคตा เคคीเคจ เคฎाเคจเค• I/O เคธ्เคŸ्เคฐीเคฎ्เคธ เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै เค•ंเคธोเคฒ เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ เค•े เคฒिเค:

1️⃣ System.out – Standard Output Stream เคฎाเคจเค• เค†เค‰เคŸเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ

  • This is used to print normal output to the console.

  • เค‡เคธเค•ा เค‰เคชเคฏोเค— เค•ंเคธोเคฒ เคชเคฐ เคธाเคฎाเคจ्เคฏ เค†เค‰เคŸเคชुเคŸ เคช्เคฐिंเคŸ เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Example / เค‰เคฆाเคนเคฐเคฃ:

public class Sample {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Output / เค†เค‰เคŸเคชुเคŸ:

Hello World

2️⃣ System.err – Standard Error Stream เคฎाเคจเค• เคเคฐเคฐ เคธ्เคŸ्เคฐीเคฎ

  • This is used to print error messages to the console.

  • เค‡เคธเค•ा เค‰เคชเคฏोเค— เค•ंเคธोเคฒ เคชเคฐ เคเคฐเคฐ เคธंเคฆेเคถ เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Example / เค‰เคฆाเคนเคฐเคฃ:

public class Sample {
    public static void main(String[] args) {
        System.err.println("Error occurred!");
    }
}

Output / เค†เค‰เคŸเคชुเคŸ:

Error occurred!

⚡ เคง्เคฏाเคจ เคฆें: เค•ुเค› IDEs เค”เคฐ เคŸเคฐ्เคฎिเคจเคฒ्เคธ เคชเคฐ เคเคฐเคฐ เคธंเคฆेเคถ เคฒाเคฒ เคฐंเค— เคฎें เคฆिเค–ाเคˆ เคฆेเคคे เคนैं।


3️⃣ System.in – Standard Input Stream เคฎाเคจเค• เค‡เคจเคชुเคŸ เคธ्เคŸ्เคฐीเคฎ

  • This is used to take input from the keyboard.

  • เค‡เคธเค•ा เค‰เคชเคฏोเค— เค•ीเคฌोเคฐ्เคก เคธे เค‡เคจเคชुเคŸ เคฒेเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Example / เค‰เคฆाเคนเคฐเคฃ:

import java.io.IOException;

public class Sample {
    public static void main(String[] args) {
        int i;
        System.out.println("Enter any value:");
        try {
            i = System.in.read();   // Reads one byte from keyboard
            System.out.println("You entered:");
            System.out.println((char) i);
        } catch (IOException e) {
            System.out.println("Reading error");
        }
    }
}

Sample Output / เคจเคฎूเคจा เค†เค‰เคŸเคชुเคŸ:

Enter any value: 5
You entered:
5

๐Ÿ’ก Key Points (เคฎुเค–्เคฏ เคฌाเคคें)

  1. System.in.read() reads one byte at a time.
    System.in.read() เคเค• เคธเคฎเคฏ เคฎें เคเค• เคฌाเค‡เคŸ เคชเคข़เคคा เคนै।

  2. For reading full strings or numbers, use Scanner or BufferedReader.
    เคชूเคฐे เคธ्เคŸ्เคฐिंเค— เคฏा เคจंเคฌเคฐ เคชเคข़เคจे เค•े เคฒिเค Scanner เคฏा BufferedReader เค•ा เคช्เคฐเคฏोเค— เค•เคฐें।

  3. System.out เค”เคฐ System.err เคฒเค—เคญเค— เคธเคฎाเคจ เคนैं, เคซเคฐ्เค• เคธिเคฐ्เคซ เคฏเคน เคนै เค•ि System.err เคเคฐเคฐ เค•ो เค†เค‰เคŸเคชुเคŸ เค•เคฐเคคा เคนै।

File Handling in Java เคœाเคตा เคฎें เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค—

File Handling in Java allows us to create, read, write, and manage files on the system. It is part of java.io package which provides multiple classes and methods to handle files.

เคœाเคตा เคฎें เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค— เค•े เคฆ्เคตाเคฐा เคนเคฎ เคธिเคธ्เคŸเคฎ เคชเคฐ เคซाเค‡เคฒ เค•ो เคฌเคจाเคจा, เคชเคข़เคจा, เคฒिเค–เคจा เค”เคฐ เคช्เคฐเคฌंเคงिเคค เค•เคฐ เคธเค•เคคे เคนैं। เคฏเคน java.io เคชैเค•ेเคœ เค•ा เคนिเคธ्เคธा เคนै เคœो เคซ़ाเค‡เคฒ เคนैंเคกเคฒिंเค— เค•े เคฒिเค เค•เคˆ เค•्เคฒाเคธ เค”เคฐ เคฎेเคฅเคก เค‰เคชเคฒเคฌ्เคง เค•เคฐाเคคा เคนै।


๐Ÿ”น Common Classes for File Handling in Java

Class Description (English) เคตिเคตเคฐเคฃ (Hindi)
File Represents file or directory path เคซ़ाเค‡เคฒ เคฏा เคกाเคฏเคฐेเค•्เคŸเคฐी เค•ा เคช्เคฐเคคिเคจिเคงिเคค्เคต เค•เคฐเคคा เคนै
FileReader Used to read data from a file character by character เคซ़ाเค‡เคฒ เค•ो เค•ैเคฐेเค•्เคŸเคฐ-เคฌाเคฏ-เค•ैเคฐेเค•्เคŸเคฐ เคชเคข़เคจे เค•े เคฒिเค
FileWriter Used to write character data to a file เคซ़ाเค‡เคฒ เคฎें เค•ैเคฐेเค•्เคŸเคฐ เคกेเคŸा เคฒिเค–เคจे เค•े เคฒिเค
BufferedReader Reads text efficiently line by line เคŸेเค•्เคธ्เคŸ เค•ो เคฒाเค‡เคจ-เคฌाเคฏ-เคฒाเค‡เคจ เคช्เคฐเคญाเคตी เคฐूเคช เคธे เคชเคข़เคคा เคนै
BufferedWriter Writes text efficiently to a file เคŸेเค•्เคธ्เคŸ เค•ो เคช्เคฐเคญाเคตी เคฐूเคช เคธे เคซ़ाเค‡เคฒ เคฎें เคฒिเค–เคคा เคนै
FileInputStream Reads binary data from a file เคฌाเค‡เคจเคฐी เคกेเคŸा เค•ो เคซ़ाเค‡เคฒ เคธे เคชเคข़เคจे เค•े เคฒिเค
FileOutputStream Writes binary data to a file เคฌाเค‡เคจเคฐी เคกेเคŸा เค•ो เคซ़ाเค‡เคฒ เคฎें เคฒिเค–เคจे เค•े เคฒिเค

1️⃣ Creating a File (เคซ़ाเค‡เคฒ เคฌเคจाเคจा)

import java.io.File;
import java.io.IOException;
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 (เค†เค‰เคŸเคชुเคŸ):

  • File created: myfile.txt

  • If file already exists → File already exists.


2️⃣ Writing to a File (เคซ़ाเค‡เคฒ เคฎें เคฒिเค–เคจा)

import java.io.FileWriter;
import java.io.IOException;
class WriteFileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("myfile.txt");
writer.write("Hello, this is Java File Handling!");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

3️⃣ Reading from a File (เคซ़ाเค‡เคฒ เคธे เคชเคข़เคจा)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class ReadFileExample {
public static void main(String[] args) {
try {
File file = new File("myfile.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
}
}
}

4️⃣ Deleting a File (เคซ़ाเค‡เคฒ เค•ो เคนเคŸाเคจा)

import java.io.File;
class DeleteFileExample {
public static void main(String[] args) {
File file = new File("myfile.txt");
if (file.delete()) {
System.out.println("Deleted the file: " + file.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}

✅ Key Points (เคฎुเค–्เคฏ เคฌाเคคें)

  • File Handling is essential for persistent storage.

  • Use FileReader/FileWriter for text data.

  • Use FileInputStream/FileOutputStream for binary data.

  • Always close file objects to avoid data loss.

  • Use try-catch blocks to handle exceptions.

Friday, August 1, 2025

Loop Control in Java Applet (เคœाเคตा เคเคช्เคฒेเคŸ เคฎें เคฒूเคช เค•ंเคŸ्เคฐोเคฒ)

In Java Applets, we can use loop control statements to create animations or repeated patterns. By using loops like for, while, or do-while, we can draw multiple shapes, apply different colors, and create moving effects.

เคœाเคตा เคเคช्เคฒेเคŸ เคฎें เคนเคฎ เคฒूเคช เค•ंเคŸ्เคฐोเคฒ เคธ्เคŸेเคŸเคฎेंเคŸ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคเคจीเคฎेเคถเคจ เคฏा เคฆोเคนเคฐाเค เคœाเคจे เคตाเคฒे เคชैเคŸเคฐ्เคจ เคฌเคจा เคธเค•เคคे เคนैं। for, while เคฏा do-while เคฒूเคช เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคนเคฎ เคเค• เคนी เค†เค•ृเคคि เค•ो เคฌाเคฐ-เคฌाเคฐ เคตिเคญिเคจ्เคจ เคฐंเค—ों เคฎें เคฌเคจाเค•เคฐ เคฎूเคตिंเค— เค‡เคซेเค•्เคŸ เคคैเคฏाเคฐ เค•เคฐ เคธเค•เคคे เคนैं।


๐ŸŒˆ Example: Color Changing Rectangles using For Loop

This example draws multiple rectangles in different shades of red using a for loop.
เคฏเคน เค‰เคฆाเคนเคฐเคฃ for เคฒूเคช เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคฒाเคฒ เคฐंเค— เค•ी เคตिเคญिเคจ्เคจ เคถेเคก्เคธ เคฎें เค•เคˆ เค†เคฏเคคें เคฌเคจाเคคा เคนै।

// File Name: ForLoopApplet.java
import java.awt.*;
import java.applet.*;

public class ForLoopApplet extends Applet {
    public void paint(Graphics g) {
        Color newColor = new Color(0,0,0);

        for(int i=0; i<10; i++) {
            newColor = new Color(i*20+55, 0, 0);  // Different red shades
            g.setColor(newColor);
            g.fillRect(i * 40, i * 10, 30, 60);   // Rectangles in a pattern
        }
    }
}

๐Ÿ”น Output Description (เค†เค‰เคŸเคชुเคŸ เคตिเคตเคฐเคฃ)

  • 10 Rectangles will appear in different shades of red. 10 เค†เคฏเคคें เคฒाเคฒ เคฐंเค— เค•े เคตिเคญिเคจ्เคจ เคถेเคก्เคธ เคฎें เคฆिเค–ाเคˆ เคฆेंเค—ी।

  • Rectangles will appear diagonally, moving slightly downward. เคฏे เค†เคฏเคคें เคคिเคฐเค›े (เคกाเคฏเค—ोเคจเคฒ) เคฐूเคช เคฎें เคฌเคจेंเค—ी।

  • Loop allows us to easily create repetitive patterns or animations. เคฒूเคช เค•ी เคฎเคฆเคฆ เคธे เคนเคฎ เคฌाเคฐ-เคฌाเคฐ เคฆोเคนเคฐाเค เคœाเคจे เคตाเคฒे เคชैเคŸเคฐ्เคจ เคฏा เคเคจीเคฎेเคถเคจ เค†เคธाเคจी เคธे เคฌเคจा เคธเค•เคคे เคนैं।

Java Graphics (AWT) - All Shapes in One Applet example program

import java.awt.*;

import java.applet.*;

public class AllGraphicsShapesApplet extends Applet {

    TextField txtShape = new TextField("0");

    int intShapeChoice;


    public void init() {

        add(txtShape);

    }

    public void paint(Graphics g) {

        g.drawString(" 1. Line", 10, 60);

        g.drawString(" 2. Rectangle", 250, 60);

        g.drawString(" 3. Filled Rectangle", 10, 80);

        g.drawString(" 4. Oval", 250, 80);

        g.drawString(" 5. Filled Oval", 10, 100);

        g.drawString(" 6. Arc", 250, 100);

        g.drawString(" 7. Filled Arc", 10, 120);

        g.drawString(" 8. Polygon", 250, 120);

        g.drawString(" 9. Filled Polygon", 10, 140);

        try {

            intShapeChoice = Integer.parseInt(txtShape.getText());

            switch(intShapeChoice) {

                case 1: g.drawLine(10,340, 400 , 340); break;

                case 2: g.drawRect(10,340, 400 ,50); break;

                case 3: g.fillRect(10,340, 400 ,50); break;

                case 4: g.drawOval(20, 340, 200, 120); break;

                case 5: g.fillOval(20, 340, 200, 120); break;

                case 6: g.drawArc(20, 340, 200, 120, 45, 45); break;

                case 7: g.fillArc(20, 340, 200, 120, 45, 45); break;

                case 8: 

                    int xPoints[] = {10, 440, 10, 10};

                    int yPoints[] = {340, 440, 180, 340};

                    g.drawPolygon(xPoints, yPoints, xPoints.length);

                    break;

                case 9: 

                    int xP[] = {10, 440, 10, 10};

                    int yP[] = {340, 440, 180, 340};

                    g.fillPolygon(xP, yP, xP.length);

                    break;

                default: g.drawString("Enter a Valid Shape Number", 260, 40);

            }

        } catch(Exception ex) {

            g.drawString("Enter a Valid Shape Number", 260, 40);

        }

    }

    public boolean action(Event evnt, Object obj){

        repaint();

        return true;

    }

}


Java Graphics (AWT) – Lamp Design Example Program

import java.awt.*;

public class Lamp extends java.applet.Applet {

    public void paint(Graphics g) {

        g.fillRect(0,250,290,290);        // Lamp platform

        g.drawLine(125,250,125,160);      // Lamp stand

        g.drawLine(175,250,175,160);


        g.drawArc(85,157,130,50,-65,312); // Bottom of shade

        g.drawArc(85,87,130,50,62,58);    // Top of shade

        g.drawLine(85,177,119,89);        // Left side

        g.drawLine(215,177,181,89);       // Right side


        g.fillArc(78,120,40,40,63,-174);  // Dots

        g.fillOval(120,96,40,40);

        g.fillArc(173,100,40,40,110,180);

    }

}


Java Swing – Introduction เคœाเคตा เคธ्เคตिंเค— เคชเคฐिเคšเคฏ, Swing Core Components เคธ्เคตिंเค— เค•े เคฎुเค–्เคฏ เค…เคตเคฏเคต

Swing, เคœाเคตा เค•ा Modern GUI (Graphical User Interface) Toolkit เคนै, เคœो เคช्เคฒेเคŸเคซॉเคฐ्เคฎ-เค‡ंเคกिเคชेंเคกेंเคŸ Desktop Applications เคฌเคจाเคจे เค•े เคฒिเค เคช्เคฐเคฏोเค— เค•िเคฏा เคœाเคคा เคนै। Swing is Java’s modern GUI toolkit used to create platform-independent desktop applications.


๐Ÿ–ฅ️ Swing เค•ी เคตिเคถेเคทเคคाเคँ / Features of Swing

  1. Lightweight Components
    Swing เค•े เคธเคญी GUI components lightweight เคนोเคคे เคนैं เค”เคฐ เคช्เคฒेเคŸเคซॉเคฐ्เคฎ เคชเคฐ เคจिเคฐ्เคญเคฐ เคจเคนीं เคนोเคคे।
    All Swing components are lightweight and platform-independent.

  2. Rich Set of Components
    Swing เคฎें JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar เค†เคฆि เคœैเคธे เคฌเคนुเคค เคธे advanced GUI components เคฎौเคœूเคฆ เคนैं।
    Swing provides advanced GUI components like JButton, JTextField, JCheckBox, JTable, JMenu, JProgressBar, etc.

  3. Pluggable Look and Feel
    Swing เคเคช्เคฒिเค•ेเคถเคจ เค•ा look and feel change เค•िเคฏा เคœा เคธเค•เคคा เคนै, เคœैเคธे Windows, Metal เคฏा Nimbus।
    Swing applications support changing their look and feel, e.g., Windows, Metal, Nimbus.

  4. MVC Architecture
    Swing components Model-View-Controller (MVC) architecture เคชเคฐ เค•ाเคฎ เค•เคฐเคคे เคนैं।
    Swing components follow the Model-View-Controller (MVC) architecture.

  5. Event-Driven Programming
    Swing เค‡เคตेंเคŸ เคก्เคฐिเคตेเคจ เคนोเคคी เคนै, เคฏाเคจी เคธเคญी actions เคœैเคธे button click, mouse hover, key press events เคธे handle เคนोเคคे เคนैं।
    Swing is event-driven, meaning actions like button clicks, mouse hovers, and key presses are handled via events.


๐Ÿ—️ Swing เค•े Common Classes

  1. JFrame → Main Window เคฌเคจाเคจे เค•े เคฒिเค।
    JFrame → Used to create the main window.

  2. JPanel → GUI components เค•ो เคฐเค–เคจे เค•े เคฒिเค।
    JPanel → Used as a container for GUI components.

  3. JButton → Button create เค•เคฐเคจे เค•े เคฒिเค।
    JButton → Used to create buttons.

  4. JLabel → Text เคฏा Image display เค•เคฐเคจे เค•े เคฒिเค।
    JLabel → Used to display text or images.

  5. JTextField → Single line text input เค•े เคฒिเค।
    JTextField → Used for single-line text input.


๐Ÿ’ป เคชเคนเคฒा Swing Program – Basic JFrame

import javax.swing.*;
class FirstSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing Example");
frame.setSize(400, 300); // width, height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:

  • เคเค• เค–ाเคฒी 400x300 JFrame Window เคฆिเค–ाเคˆ เคฆेเค—ी।


Key Points:

  • Swing classes javax.swing package เคฎें เคนोเคคी เคนैं।

  • JFrame เคชเคฐ components เคœोเคก़เคจे เค•े เคฒिเค add() method เค•ा use เค•เคฐเคคे เคนैं।

  • เคเคช्เคฒिเค•ेเคถเคจ เค•ो เคฌंเคฆ เค•เคฐเคจे เค•े เคฒिเค setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) เคœเคฐूเคฐी เคนै। 


๐ŸŒŸ Java Swing Core Components


1️⃣ JFrame – Main Window

JFrame is the main window in any Swing application. It acts as a container that holds all GUI components like buttons, labels, and text fields.

JFrame, เค•िเคธी เคญी Swing เคเคช्เคฒिเค•ेเคถเคจ เค•ी เคฎुเค–्เคฏ เคตिंเคกो เคนोเคคी เคนै। เคฏเคน เคเค• เค•ंเคŸेเคจเคฐ เค•ी เคคเคฐเคน เค•ाเคฎ เค•เคฐเคคी เคนै, เคœिเคธเคฎें เคฌเคŸเคจ, เคฒेเคฌเคฒ เค”เคฐ เคŸेเค•्เคธ्เคŸ เคซीเคฒ्เคก เคœैเคธे เคธเคญी GUI components เคฐเค–े เคœाเคคे เคนैं।

Key Points:

  • JFrame is top-level container.

  • Use setSize(width, height) to define its size.

  • Use setVisible(true) to make it visible.

  • Close operation must be set with setDefaultCloseOperation().

Example:

import javax.swing.*;

class JFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My JFrame Example");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

2️⃣ JPanel – Sub Container

JPanel is a sub-container used inside a JFrame to organize GUI components. It helps in grouping elements and managing layouts.

JPanel, JFrame เค•े เค…ंเคฆเคฐ เคช्เคฐเคฏोเค— เคนोเคจे เคตाเคฒा เคธเคฌ-เค•ंเคŸेเคจเคฐ เคนै। เค‡เคธเค•ा เค‰เคชเคฏोเค— elements เค•ो group เค•เคฐเคจे เค”เคฐ layout manage เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Key Points:

  • JPanel is invisible by default.

  • You can add multiple JPanels in a JFrame.

  • It supports layouts like FlowLayout, BorderLayout, GridLayout.

Example:

import javax.swing.*;
import java.awt.*;

class JPanelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JPanel Example");
        JPanel panel = new JPanel(); // create panel

        panel.add(new JButton("Click Me")); // add button in panel

        frame.add(panel);  // add panel to frame
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3️⃣ JButton – Button Component

JButton is a clickable button that performs an action when clicked.

JButton เคเค• เค•्เคฒिเค• เค•เคฐเคจे เคฏोเค—्เคฏ เคฌเคŸเคจ เคนै, เคœिเคธ เคชเคฐ เค•्เคฒिเค• เค•เคฐเคจे เคชเคฐ เค•ोเคˆ เค•ाเคฐ्เคฏ (action) perform เคนोเคคा เคนै।

Key Points:

  • Use new JButton("Text") to create a button.

  • Events are handled using ActionListener.

Example:

import javax.swing.*;
import java.awt.event.*;

class JButtonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JButton Example");
        JButton button = new JButton("Click Me");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button Clicked!");
            }
        });

        frame.add(button);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

4️⃣ JLabel – Display Text or Image

JLabel is used to display static text, image, or both in a Swing window.

JLabel เค•ा เคช्เคฐเคฏोเค— เคธ्เคฅिเคฐ เคŸेเค•्เคธ्เคŸ, เค‡เคฎेเคœ เคฏा เคฆोเคจों เค•ो Swing เคตिंเคกो เคฎें เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Key Points:

  • JLabel cannot take input; it is read-only.

  • Supports image icons with text.

Example:

import javax.swing.*;

class JLabelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JLabel Example");
        JLabel label = new JLabel("Welcome to Java Swing!");
        
        frame.add(label);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

5️⃣ JTextField – Single Line Input Field

JTextField is used to take single-line input from the user in a Swing application.

JTextField เค•ा เค‰เคชเคฏोเค— เคฏूเคœ़เคฐ เคธे เคเค• เคฒाเค‡เคจ เค•ा เค‡เคจเคชुเคŸ เคฒेเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Key Points:

  • Use new JTextField(columns) to create text field.

  • Use getText() to read input and setText() to set text.

Example:

import javax.swing.*;

class JTextFieldExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTextField Example");
        JTextField textField = new JTextField(20);
        
        frame.add(textField);
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

๐ŸŽฏ Summary

Component Purpose / เค‰เคฆ्เคฆेเคถ्เคฏ
JFrame Main Window (เคฎुเค–्เคฏ เคตिंเคกो)
JPanel Sub-container for GUI components
JButton Clickable Button (เค•्เคฒिเค• เค•เคฐเคจे เคตाเคฒा เคฌเคŸเคจ)
JLabel Display text or image
JTextField Take single-line input from user


Example:- Java Swing Stylish Login Form with Background Image

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class StylishLoginForm extends JFrame {
    StylishLoginForm() {
        // Frame Settings
        setTitle("Stylish Login Form");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);

        // Background Image
        JLabel background = new JLabel(new ImageIcon("background.jpg"));
        background.setBounds(0, 0, 500, 300);
        setContentPane(background);
        setLayout(null);

        // Font for Labels and TextFields
        Font font = new Font("Arial", Font.BOLD, 16);

        // Username Label
        JLabel userLabel = new JLabel("Username:");
        userLabel.setBounds(80, 70, 100, 30);
        userLabel.setForeground(Color.WHITE);
        userLabel.setFont(font);
        add(userLabel);

        // Username Field
        JTextField userText = new JTextField();
        userText.setBounds(200, 70, 200, 30);
        userText.setFont(font);
        add(userText);

        // Password Label
        JLabel passLabel = new JLabel("Password:");
        passLabel.setBounds(80, 120, 100, 30);
        passLabel.setForeground(Color.WHITE);
        passLabel.setFont(font);
        add(passLabel);

        // Password Field
        JPasswordField passText = new JPasswordField();
        passText.setBounds(200, 120, 200, 30);
        passText.setFont(font);
        add(passText);

        // Login Button
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(200, 180, 100, 35);
        loginButton.setBackground(new Color(30, 144, 255));
        loginButton.setForeground(Color.WHITE);
        loginButton.setFont(new Font("Arial", Font.BOLD, 16));
        add(loginButton);

        // Button Action
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passText.getPassword());

                if (username.equals("admin") && password.equals("1234")) {
                    JOptionPane.showMessageDialog(null, 
                        "Login Successful! Welcome " + username);
                } else {
                    JOptionPane.showMessageDialog(null, 
                        "Invalid Username or Password!",
                        "Login Error",
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        new StylishLoginForm();
    }
}

๐Ÿ“ How It Works / เคฏเคน เค•ैเคธे เค•ाเคฎ เค•เคฐเคคा เคนै

  1. Background image is applied using a JLabel with ImageIcon.

  2. Custom font & colors are used to make it attractive.

  3. Button color is changed using setBackground() and setForeground().

  4. JOptionPane shows messages on login success or failure.

๐ŸŽจ Output Look

+---------------------------------------+
|     [ Background Image Visible ]       |
|  Username: [__________]                |
|  Password: [__________]                |
|               [ Login ]                |
+---------------------------------------+

๐Ÿ”บ Draw Polygon เค”เคฐ Polyline in Java (Graphics Class)

Polygon เค”เคฐ Polyline เคœाเคตा เค—्เคฐाเฅžिเค•्เคธ เคช्เคฐोเค—्เคฐाเคฎिंเค— เคฎें เคเคธे shapes เคนैं เคœिเคจเคฎें เค•เคˆ connected lines เคนोเคคी เคนैं।

In Java Graphics programming, Polygons and Polylines are shapes formed using multiple connected lines.

  • Polygon (เคฌเคนुเคญुเคœ) → Closed shape (เคถुเคฐुเค†เคค เค”เคฐ เค…ंเคค เคœुเฅœे เคนोเคคे เคนैं)

  • Polyline (เคชॉเคฒीเคฒाเค‡เคจ) → Open shape (เคถुเคฐुเค†เคค เค”เคฐ เค…ंเคค เคœुเฅœे เคจเคนीं เคนोเคคे)


1️⃣ Polygon in Java

เคœाเคตा เคฎें polygon เคฌเคจाเคจे เค•े เคฒिเค Graphics Class เค•े drawPolygon() เค”เคฐ fillPolygon() methods เค•ा เค‰เคชเคฏोเค— เคนोเคคा เคนै।
To draw polygons in Java, we use Graphics Class methods drawPolygon() and fillPolygon().

Syntax / เคช्เคฐाเคฐूเคช:

g.drawPolygon(int xPoints[], int yPoints[], int nPoints);
g.fillPolygon(int xPoints[], int yPoints[], int nPoints);
  • xPoints[] → X-axis เค•े เคธเคญी points

  • yPoints[] → Y-axis เค•े เคธเคญी points

  • nPoints → เค•िเคคเคจे points polygon เคฎें เคนोंเค—े


Example 1: Draw a Triangle

import java.applet.Applet;
import java.awt.*;

public class PolygonExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {100, 150, 50};
        int y[] = {50, 150, 150};
        g.setColor(Color.RED);
        g.drawPolygon(x, y, 3);
    }
}

Output:

  • เคเค• Red Triangle เคธ्เค•्เคฐीเคจ เคชเคฐ เคฆिเค–ाเคˆ เคฆेเค—ा।


Example 2: Filled Polygon (Pentagon)

import java.applet.Applet;
import java.awt.*;

public class FillPolygonExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {100, 150, 200, 175, 125};
        int y[] = {150, 100, 150, 200, 200};
        g.setColor(Color.BLUE);
        g.fillPolygon(x, y, 5);
    }
}

Output:

  • เคฏเคน เคเค• Blue Filled Pentagon เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐेเค—ा।


2️⃣ Polyline in Java

Polyline, polygon เค•ी เคคเคฐเคน เคนोเคคी เคนै เคฒेเค•िเคจ open shape เคนोเคคी เคนै।
A polyline is like a polygon but remains open (does not connect last point to the first).

Syntax / เคช्เคฐाเคฐूเคช:

g.drawPolyline(int xPoints[], int yPoints[], int nPoints);

Example 3: Draw Polyline

import java.applet.Applet;
import java.awt.*;

public class PolylineExample extends Applet {
    public void paint(Graphics g) {
        int x[] = {50, 100, 150, 200, 250};
        int y[] = {200, 150, 100, 150, 200};
        g.setColor(Color.MAGENTA);
        g.drawPolyline(x, y, 5);
    }
}

Output:

  • เคเค• V-shape wave line (zigzag) เคธ्เค•्เคฐीเคจ เคชเคฐ เคฌเคจेเค—ी।


Key Points to Remember

  • Polygon เคนเคฎेเคถा closed shape เคนोเคคी เคนै।

  • Polyline open shape เคนोเคคी เคนै।

  • fillPolygon() polygon เค•ो color fill เค•เคฐเคคा เคนै।

  • Points arrays เค•ा size เคนเคฎेเคถा nPoints เค•े เคฌเคฐाเคฌเคฐ เคนोเคจा เคšाเคนिเค।

Draw Arc and Filled Arc in Java (Graphics Class)

In Java, we can draw shapes like arcs or semi-circles using Graphics Class methods drawArc() and fillArc().
เคœाเคตा เคฎें Arc เคฏा Semi-Circle เคœैเคธी เค†เค•ृเคคिเคฏाँ เคฌเคจाเคจे เค•े เคฒिเค Graphics Class เค•े drawArc() เค”เคฐ fillArc() methods เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœाเคคा เคนै।


1️⃣ drawArc() Method

เคฏเคน เคฎेเคฅเคก เค•ेเคตเคฒ arc เค•ी outline เคฌเคจाเคคा เคนै।
This method only draws the outline of an arc.

Syntax / เคช्เคฐाเคฐूเคช:

g.drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
  • x, y → เคตเคน เคชॉเค‡ंเคŸ เคœเคนां เคธे rectangle เค•ा top-left corner เคถुเคฐू เคนोเค—ा

  • width, height → arc เค•ो เค˜ेเคฐเคจे เคตाเคฒे rectangle เค•ी เคšौเคก़ाเคˆ เค”เคฐ เคŠँเคšाเคˆ

  • startAngle → arc เค•เคนाँ เคธे เคถुเคฐू เคนोเค—ा (0° = 3 o’clock direction)

  • arcAngle → arc เค•ा degree (positive = anticlockwise, negative = clockwise)


2️⃣ fillArc() Method

เคฏเคน เคฎेเคฅเคก arc เค•ो color เคธे เคญเคฐ เคฆेเคคा เคนै
This method fills the arc with the current color.

Syntax / เคช्เคฐाเคฐूเคช:

g.fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);

Example 1: Draw Simple Arc in Java Applet

import java.applet.Applet;
import java.awt.*;

public class ArcExample extends Applet {
    public void paint(Graphics g) {
        g.drawArc(50, 50, 100, 100, 0, 90);     // 1st arc
        g.drawArc(200, 50, 100, 100, 0, -90);   // 2nd arc
        g.drawArc(350, 50, 100, 100, 45, 180);  // 3rd arc
    }
}

Output:

  • เคชเคนเคฒा arc → 0° เคธे 90° (anticlockwise)

  • เคฆूเคธเคฐा arc → 0° เคธे -90° (clockwise)

  • เคคीเคธเคฐा arc → 45° เคธे 180°


Example 2: Filled Arc (Pie Shape)

import java.applet.Applet;
import java.awt.*;

public class FillArcExample extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillArc(50, 50, 150, 150, 0, 120);   // Red Pie
        g.setColor(Color.GREEN);
        g.fillArc(50, 50, 150, 150, 120, 120); // Green Pie
        g.setColor(Color.BLUE);
        g.fillArc(50, 50, 150, 150, 240, 120); // Blue Pie
    }
}

Output:

  • เคฏเคน เคเค• RGB Pie Chart เคคैเคฏाเคฐ เค•เคฐेเค—ा, เคœो 3 เคฐंเค—ों เค•े arc เคธे เคฌเคจा เคนोเค—ा।


Example 3: Moving Fan using Arc

import java.awt.*;
import java.applet.*;

public class MovingFan extends Applet implements Runnable {
    int angle = 0;
    Thread t;

    public void init() {
        t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            while (true) {
                angle += 20;
                repaint();
                Thread.sleep(100);
            }
        } catch (Exception e) {
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillOval(100, 100, 200, 200); // fan boundary
        g.setColor(Color.BLUE);
        g.fillArc(100, 100, 200, 200, angle, 30);
        g.fillArc(100, 100, 200, 200, angle + 120, 30);
        g.fillArc(100, 100, 200, 200, angle + 240, 30);
    }
}

Output:

  • เคฏเคน เคเค• rotating fan animation เคคैเคฏाเคฐ เค•เคฐेเค—ा।


Key Points to Remember

  • drawArc() เค•ेเคตเคฒ arc เค•ी outline เคฌเคจाเคคा เคนै।

  • fillArc() arc เค•ो color fill เค•เคฐเคคा เคนै।

  • Angle เค•ी unit เคนเคฎेเคถा degree เคนोเคคी เคนै।

  • Animation เค•े เคฒिเค Thread เค”เคฐ repaint() เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœाเคคा เคนै।

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