Home > Java > javaTutorial > body text

Java IOException

王林
Release: 2024-08-30 16:13:56
Original
881 people have browsed it

Java IOException or IOException usually provides help for some system input and output, which are through the data streams, file system and the serialization etc. This is the method of the java.util.scanner java class which actually returns the IOException, and it is the last one thrown by some Scanner’s underlying Readable. This IOException method will return the NULL value only if no such exception actually exists.

ADVERTISEMENT Popular Course in this category Java IO Tutorial

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

Public IOException ioException()
Copy after login

A return value of the IOException:

The IOException method/function will return the last exception, which is actually thrown by the scanner’s readable/readables.

How IOException Work in Java?

The IOException of the Java language works by providing some help for some of the inputs and outputs which are available throughout the data streams, serialization and the file system. The IoException() method/functioin works by returning the last thrown Scanner’s underlying readable concept of the IOException. The IOException() method/function will return the NULL value only if no exception is available at all.

How to Avoid IOException?

Using the Try/Catch concept of the Java Programming Language, we can handle the IOException . This is a concept of avoiding the IOException.

Constructors of IOException in Java

Usually, the constructor helps in initializing an object just after the creation of the object creation. It is similar/same syntactically to a specific method/function, but the constructor is having some difference. That is having the same as its class which is having no return type. Actually, there is no need for any constructors invoking explicitly, and these will be invoked automatically at instantiation time.These constructors throw some exceptions.

1. IOException(): This is the normal constructor that constructs one of the new IOException and its stack tracing, which is to be filled in.

2. IOException(Throwable): The Throwable constructor helps in constructing one of the new class instance along with its detailed cause, which is going to filled in. Here “Throwable” parameter is the cause.

3. IOException(String): The String constructor of the IOException() helps construct one of the new IOException along with its stack tracing and with the detailed message: filled.

4. IOException(IntPtr, JniHandleOwnership): This constructor helps in creating the managed representations of some JNI objects, and the runtime calls them. The IntPtr will contain some Java Native Interface (JNI) for the object reference purpose. The JniHandleOwnership parameter indicates handling the javaReference.

5. IOException(String, Throwable): The constructor helps in constructing one of the new class instance along with some detailed message and with cause filling. Here, the String parameter is the message, and the Throwable parameter is the cause.

Examples to Implement of Java IOException

Below are the examples of Java IOException:

Example #1

This is the java program example of implementing the illustration of the ioException() method/function of some Scanner class/classes in the Java Programming Language without using any parameter. Here at first, java util is imported to use all the libraries of the Java Programming Language. Then pavankumarsake1 class is created with the Exception throwing concept. Then s1 string variable is created with the string value. Then scanner1 variable is created with the help of the s1 string variable. Then scanner.nextLine() is created to print the new line, and then scanner.ioException() is used to check whether IO Exception is present or not. Then scanner will be closed using a scanner.close() function.

Code:

import java.util.*;
public class pavansake1 {
public static void main(String[] args)
throws Exception
{
System.out.println(" \n ");
String s1 = "Hey! I'am from EDUCBA";
Scanner scanner1 = new Scanner(s1);
System.out.println("" + scanner1.nextLine());
System.out.println("" + scanner1.ioException());
scanner1.close();
}
}
Copy after login

Output:

Java IOException

Example #2

This is the Java example of illustrating the ioException() method along with some of its constructors of the Scanner class in the Java Programming Language without the parameter mentioning. Here at first, java.util.* is imported to import all the functions of the library. Then public class “pavansake1” is created by throwing the exception concept. In the throwing exception, a string s11 is created with some string value. Then a scanner variable is created with the help of the s11 variable to use it as an alternative similar one. Then the ioException() variable is used to show only if there is some IO exception.

Code:

import java.util.*;
public class pavansake1 {
public static void main(String[] argv)
throws Exception
{
System.out.println("\n");
String s11 = "EDUCBA EDUCBA!!!";
Scanner scanner11 = new Scanner(s11);
System.out.println("" + scanner11.nextLine());
System.out.println("" + scanner11.ioException());
scanner11.close();
}
}
Copy after login

Output:

Java IOException

Example #3

This is the example of implementing the IOException along with the constructor in it, but this example will provide a compilation error due to an issue. Here a class “Employee1” is created whose constructor will throw one of the IOException, and it is instantiating the class which is not handling our exception. So the compilation leads to the compile run time error. Some Java libraries are imported at first, and then private class and public class are created to handle the IOException, but here exception is not handled, so the error occurred.

Code :

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee1{
private String name1;
private int age1;
File empFile1;
Employee1(String name1, int age1, String empFile1) throws IOException{
this.name1 = name1;
this.age1 = age1;
this.empFile1 = new File(empFile1);
new FileWriter(empFile1).write("Employee name is "+name1+"and age is "+age1);
}
public void display(){
System.out.println("Name: "+name1);
System.out.println("Age: "+age1);
}
}
public class ConstructorExample1 {
public static void main(String args[]) {
String filePath1 = "samplefile.txt";
Employee emp1 = new Employee("pksake", 25, filePath);
}
}
Copy after login

Output:

Java IOException

Example #4

This is the example that is very much similar to example 3. To make example 3 to work, we are wrapping the line’s instantiation within the try-catch or the throw exception.

Code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee1{
private String name1;
private int age1;
File empFile1;
Employee1(String name1, int age1, String empFile1) throws IOException{
this.name1 = name1;
this.age1 = age1;
this.empFile1 = new File(empFile1);
new FileWriter(empFile1).write("Employee name is "+name1+"and age is "+age1);
}
public void display(){
System.out.println("Name: "+name1);
System.out.println("Age: "+age1);
}
}
public class Employee11 {
public static void main(String args[]) {
String filePath1 = "file1.txt";
Employee1 emp1 = null;
try{
emp1 = new Employee1("pksake", 26, filePath1);
}catch(IOException ex1){
System.out.println("The specific file will not be found");
}
emp1.display();
}
}
Copy after login

Output :

Java IOException

Conclusion

I hope you have learned the definition of Java IOException and its syntax and explanation, How the IOException works in Java Programming Language and its constructors, Java IOException examples, and How to avoid the IOException, etc.

The above is the detailed content of Java IOException. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!