FileNotFoundException in Java

FileNotFoundException:

java.io.FileNotFoundException

Signals that an attempt to open the file denoted by a specified path name has failed.

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

Example: 

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class FileNotFoundExceptionExample {
    public static void main(String[] args) throws IOException {

        String pathname = "hello";
        File file = new File(pathname);

        try {
            InputStream in = new FileInputStream(file);
            System.out.println("File exist");
            in.close();
        } catch (FileNotFoundException e) {
            System.out.println("file does not exist");
        }
    }
}

Output:

file does not exist


Constructor of FileNotFoundException

Example 1:

java.io.FileNotFoundException.FileNotFoundException()

Constructs a FileNotFoundException with null as its error detail message.

Java

import java.io.FileNotFoundException;
import java.io.IOException;

public class FileNotFoundExceptionExample {
    public static void main(String[] args) throws IOException {

        FileNotFoundException fileNotFoundException =
new FileNotFoundException();

        System.out.println(fileNotFoundException);
    }
}

Output:

java.io.FileNotFoundException


Example 2:

java.io.FileNotFoundException.FileNotFoundException(String s)

Constructs a FileNotFoundException with the specified detail message. The string s can be retrieved later by the java.lang.Throwable.getMessagemethod of class java.lang.Throwable.

Parameters:

1. s the detailed message.

Java

import java.io.FileNotFoundException;
import java.io.IOException;

public class FileNotFoundExceptionExample {
    public static void main(String[] args) throws IOException {

        FileNotFoundException fileNotFoundException =
new FileNotFoundException("hello");

        System.out.println(fileNotFoundException);
    }
}

Output:

java.io.FileNotFoundException: hello


Some other Exceptions of java.io

EOFExceptionSignals that an end of file or end of the stream has been reached unexpectedly during input.

InterruptedIOExceptionSignals that an I/O operation has been interrupted. 

InvalidClassExceptionThrown when the Serialization runtime detects.

InvalidObjectExceptionIndicates that one or more deserialized objects failed validation tests.

IOErrorThrown when a serious I/O error has occurred.

IOExceptionSignals that an I/O exception of some sort has occurred.

NotActiveExceptionThrown when serialization or deserialization is not active.

NotSerializableExceptionThrown when an instance is required to have a Serializable interface.

ObjectStreamExceptionSuperclass of all exceptions specific to Object Stream classes.

OptionalDataExceptionException indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream.

StreamCorruptedExceptionThrown when control information that was read from an object stream violates internal consistency checks.

SyncFailedExceptionSignals that a sync operation has failed.

UncheckedIOExceptionWraps an IOException with an unchecked exception.

UnsupportedEncodingExceptionCharacter Encoding is not supported.

UTFDataFormatExceptionSignals that a malformed string in modified UTF-8 format has been read in a data input stream or by any class that implements the data input interface.

WriteAbortedExceptionSignals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment