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
-
सुरक्षित रूप से फाइल पढ़ना और लिखना
-
नेटवर्क आधारित एप्लीकेशन में एरर हैंडलिंग
-
फाइल या डिवाइस फेल होने पर प्रोग्राम को क्रैश होने से बचाना
Comments
Post a Comment