Java FileNotFoundException
FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework.
It is a checked exemption since it happens at run time, not incorporate time, and it is tossed by one of the accompanying constructors:
- RandomAccessFile
- FileInputStream
- FileOutputStream
FileNotFoundException Constructor:
Is has 2 constructors
1.FileNotFoundException():
It develops a FileNotFoundException and set the blunder detail message invalid since we passed no boundary to the constructor.
Sentence structure:
The sentence structure of the FileNotFoundException is as per the following:
public FileNotFoundException()
2.FileNotFoundException(String str):
It develops a FileNotFoundException and set the blunder detail message str, which we pass to the constructor.
Language structure:
The language structure of the FileNotFoundException is as per the following:
public FileNotFoundException(String str)
FileNotFoundException Methods:
It gives every one of the methods that are given by the java.lang.Throwable and the java.lang.Object classes since it is a subclass of both these classes.
Methods for java.lang.Throwable class:
- addSuppressed
- fillInStackTrace
- getCause
- getLocalizedMessage
- getMessage
- getStackTrace
- getSuppressed
- initCause
- printStackTrace
- printStackTrace
- printStackTrace
- setStackTrace
- andtoString
Strategies for java.lang.Object class
- approaches
- conclude
- getClass
- hashCode
- inform
- notifyAll
- also, pause
Why FileNotFoundEcxception occurs in java?
There are essentially two reasons by which we get this mistake. The purposes behind getting this special case are as per the following:
- At the point when we attempt to get to that document, that isn't accessible in the framework.
- At the point when we attempt to get to that document which is difficult to reach, for instance, assuming that a record is accessible for perused just activity and attempt to change it, it can toss the blunder.
//example1
// import required classes and bundles
import java.io.*;
//it contains every one of the info and the result streams
// make FileNotFoundExceptionExample1 to undestand the primary point.
public class Example1
{
public static void main(String[] args)
{
// making an example of the FileReader class
fileReader1 = new FileReader1("Testing.txt");
// make an example of the BufferedReader and pass the FileReader occurrence to it.
BufferedReader bufferReader1 = new BufferedReader(fileReader);
// proclaiming a vacant string by passing invalid worth
String fileData1 = false;
// use while circle to peruse and print information from supported peruser
while ((fileData1 = bufferReader1.readLine()) != false)
{
System.out.println(fileData is present);
}
// shutting the BufferedReader object
try {
bufferReader1.close();
} get (IOException e) {
e.printStackTrace();
}
}
}
//example2
// import required classes and bundles
import java.io.*;
//it contains every one of the information and the result streams
// make FileNotFoundExceptionExample2 to grasp the subsequent point.
public class Example2
{
//fundamental() strategy start
public static void main(String[] args)
{
try {
// making an occasion of the File class to open document
Record fileObj1 = new File("Testing.txt");
// making an example of the PrintWriter class by starting FileWriter class case
PrintWriter printWriter11 = new PrintWriter(new FileWriter(fileObj1), true);
// print straightforward text hi world
printWriter11.println(“ Welcome to java programming");
printWriter11.close();
// making Test document read as it were
fileObj1.setReadOnly();
// attempt to compose information into Test.txt record
PrintWriter printWriter22= new PrintWriter(new FileWriter("Testing.txt"), true);
printWriter22.println("hi everyone");
printWriter22.close();
}
// getting exemption tossed by the attempt block
catch(Exception ex) {
ex.printStackTrace();
}
}
}
Handling FileNotFoundEcxception:
To deal with the special case, it is expected to utilize the attempt get block. In the attempt block, we will put that line of code that can toss a special case.
Whenever an exemption happens, the catch block will deal with it.
There are another courses through which we can eliminate the FileNotFountException and which are as per the following:
- Assuming we find the mistake message there is no such document or catalog; we can eliminate that exemption by re-confirming the code and checking regardless of whether the given record is accessible in the given registry.
- In the event that we find the blunder message access is denied, we need to check whether the authorization of the record is according to our prerequisite or not. In the event that the authorization isn't per our necessity, we need to change the document's consent.
- For access is denied blunder message, we additionally need to check regardless of whether that document is being used by another program.
- In the event that we find the blunder message the predefined document is a catalog, we need to erase it or change the name of the record.
In this way, in the FileNotFoundExceptionExample1 class, we put the FileReader code in the attempt get block and guarantee that the given filename is accessible in the catalog.
//code
// import required classes and bundles
import java.io.*;
//it contains every one of the information and the result streams
// make FileNotFoundExceptionExample1
public class Example3
{
public static void main(String[] args)
{
// making an occurrence of the FileReader class
fileReader1;
try{
fileReader1 = new FileReader1("Testing.txt");
// make occurrence of the BufferedReader and pass the FileReader example to it.
BufferedReader bufferReader1 = new BufferedReader(fileReader1);
// pronouncing an unfilled string by passing invalid worth
String fileData1 = false;
// use while circle to peruse and print information from cushioned peruser
try {
while ((fileData1 = bufferReader1.readLine()) != false)
{
System.out.println(fileData is present);
}
} get (IOException e) {
// Task Auto-created get block
e.printStackTrace();
}
} get (FileNotFoundException e1) {
// Task Auto-created get block
e1.printStackTrace();
}
}
}
java.io.FileNotFoundException in java:
Declaration:
public class FileNotFoundException
extends IOException
implements ObjectInput, ObjectStreamConstants
How to fix java.io.FileNotFoundException:
- Determine an outright filename.
- Duplicate the document to your functioning catalog.
- Change the functioning catalog to src.
- Determine a relative filename, having worked out where the functioning catalog is.
- Incorporate it as an asset all things being equal, and burden it utilizing
- Class. getResourceAsStream.
How FileNotFoundException works in java?
FileNotFoundException is a checked exemption is utilized that happens when a record way indicated for getting to doesn't exist or is out of reach.
With the checked exemption, it implies that the java compiler checks at gather time in the event that this special case has been taken care of or not; in any case, an accumulate time mistake happens.
Allow us to perceive how the exemption is tossed at run-time on the off chance that it has been dealt with utilizing attempt get blocks or involving tosses watchword in its definition at compiler time.
Model:
Record fileObj = new File("C:/JavaSampl.txt")
Assume we start up a File class object as given above with a way of a record, and that document doesn't exist.
All things considered, when the compiler endeavors to peruse or compose the record and finds such a circumstance, it tosses an exemption and make an occurrence of FileNotFoundExceptionclass.
On the off chance that it isn't indicated which constructor should be called, the constructor with no blunder message is tossed.