Java

Java IOException

“Java IOException, also known as IOException, frequently offers assistance for various system input and output, including through data streams, the file system, serialization, etc. The latest IOException to be issued by some Scanner’s underlying Readable is returned by this method of the Java class java.util.scanner. Only if there is no such exception, this IOException function will return NULL. The category of checked exceptions includes the IOExceptions. The exceptions that are checked are those that arise while the Java program is being built. IOException has numerous subclasses, including FileNotFound Exception and SSL Exception.”

Syntax of IOException in Java in Ubuntu 20.04

The IOException syntax is as follows:

Public IOException ioException()

The last exception, which is thrown by the scanner readable, will be returned by the IOException method or function. Only in cases when there is absolutely no exception available will the IOException() function return a NULL value.

Example 1: Raising IOException in Java in Ubuntu 20.04

Let’s examine a really basic Java IOException scenario. In the example that follows, we will try to read a few lines of text from a file that doesn’t exist.

Inside class “IOException”, we have accessed the main method. This method invoked the class “Exception” with the throws modifier. The main method throws an error when any input-output occurs here. Then, we defined the variable “FileName,” where we have given the wrong file

name, “Java.txt,” that doesn’t exist. After that, we have an object, “FIS,” which creates the file input stream. The system.out.print raises the exception of FileNotFound.

When we compile the above java program, we will get an Exception, i.e., FileNotFound Exception. This is the type of IOException that occurs due to the reading of an invalid path location file.

Example 2: Raising EndOfFile Exception in Java in Ubuntu 20.04

An IOException subclass includes EOF Exception. The code as follows demonstrates how an EndOfFile Exception can occur when reading an input file. An EOF Exception is triggered when a file reaches its end during reading.

Inside the header section, we have loaded the package “java.io.IOException”. Then, we have a class, “EOF,” where the VerifyEOF() method is defined. Inside that, we have a variable “fn” where the file path “File.txt” is given. After that, we have a try-catch block where the try block will read the input stream of the file, and the catch block will raise the EOF error when it reaches the end of the file. Next, we have finally block where we have try-catch block again. The main method is implemented at the end that declares the instance “obj” of the “EOF” class which returns the results of the VerifyEOF method.

Upon interpretation of the above-specified file, the EndOfFile Exception appears on the terminal.

Example 3: Raising FileNotFound Exception in Java in Ubuntu 20.04

The subclass of IOException that FileNotFound Exception belongs to. The code below displays an example of a FileNotFound Exception. If the input file is missing, this error occurs.

Here, we have a “FOE” class where the method is established as the “IsFileNotFound” method. The “IsFileNotFound” method has the try-catch block. We have placed the file operation in the try block, as we know that an IOException raises here. Within the variable “file”, we have set the file name. After that, the catch block, which is positioned below the try block, will handle any IOException that happens in the try block.

As the file does not exist, the “FileIsNotFound” exception is returned in the following output.

How to Solve These IOException in Java

When an IO process fails, Java throws the IOException exception. The root reason for the failure can be printed out, and the exception can be explicitly handled by developing a try-catch-finally block. Adding more code to the try-catch blocks will enable the developer to take the appropriate steps to resolve this issue.

Example 4: Using the Try-Catch Block for IOException Handling in Java in Ubuntu 20.04

One of the most effective ways to prevent exceptions, whether they are checked or unchecked, is to use the try-and-catch mechanism. Let’s explore how the try-catch block may be used to handle the Java ioexception.

We have assigned the name “solution1” to the java class of this example. We have created the main method along with the Exception method. Inside the main method, we have implemented the try-catch block. The try section takes the file name inside the variable “FileIs,” and the variable “File” gets the input stream from the specified file path. If the file doesn’t occur, then the error is raised that will be thrown by the catch block. The catch block printed the error which we have set against the IO error thrown by the compiler by default.

We have explicitly printed the IOException of FileNotFound with the message as shown:

Example 5: Using the “UnsupportedEncodingException” Method for IOException Handling in Java in Ubuntu 20.04

Let’s analyze the different IOExceptions samples.

  • FileNotFound Exception: The FileNotFound Exception has already been covered in the article’s section above.
  • UnsupportedEncoding Exception: This error is raised when an improper encoding scheme is used to try to encrypt the String.

We have defined the class as “solution2”. In the class main method, we have declared the variable “myStr,” and the variable “myStr” is set with the string. Then, we have an object as “bytes” for the byte[] array method. The byte object has utilized the wrong format for the string encoding, so the “UnsupportedEncoding Exception” error will be raised.

When the code complies with the class name “solution2”, it throws the unsupported encoding exception on the screen as follows:

Conclusion

Failures that occur when reading, writing, or scanning a file or directory must be handled by the Java module “java.io”. The default exception class for handling errors is IOException. Try, catch, and finally block in a class method taking care of handling the exception. An IOException or one of its subclasses may be thrown by application API class functions. Now, you understand the IOException, how it works, and its syntax. We have given some samples to use Java IOException and how to prevent them in java.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.