• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » FileNotFoundException in Java Exception Handling And Resolution Example

FileNotFoundException in Java Exception Handling And Resolution Example

Last Updated on: April 13, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Possible Reasons For FileNotFound Exception
  • FileNotFoundException in Java
  • How to Deal FileNotFound Exception

FileNotFoundException In Java: In this article, we’re going to talk about a very common exception in Java – the FileNotFoundException. we can get this exception when we try to access the file but the file is not present in that location,

Possible Reasons For FileNotFound Exception

There are a few possible reasons for getting this type of exception, here are some:

  • A File may be present or not in the mentioned path
  • A file with the specified pathname does exist but is inaccessible for some reason (requested writing for a read-only file, or permissions don’t allow accessing the file)

FileNotFoundException in Java

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.

Check Also: Scientific Games Interview Questions

we will get this exception when we are trying to access a file that does not exist. for access to the files, we are using classes like  FileInputStream, FileOutputStream, and RandomAccessFile. The main function of these classes is used to obtain the bytes from a file system.

package co.java.exception;
import java.io.File;
import java.io.FileReader;
public class FileNotFoundException 
{
   @SuppressWarnings("unused")
   public static void main(String[] args) throws java.io.FileNotFoundException
   {
      File file = new File("E://file.txt");
        @SuppressWarnings("resource")
      FileReader fr = new FileReader(file);
   }
}

Let’s Run the above program

After execution we get an error:

Exception in thread "main" java.io.FileNotFoundException: E:\file.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at co.java.exception.FileNotFoundException.main(FileNotFoundException.java:12)

How to Deal FileNotFound Exception

  • Once you got this exception than the first thing you need to check the path which you mentioned in your program to verify that the specified file is present or not in the mentioned path.
  • if the file is present in the specified location but still you got the error then you need to check error message for confirming is there any mention about permission related issues if there are some permission related issues then you need to fix that issue or you need to verify that file is used by other application or not.
  • Alert a user with a dialogue or error message: this isn’t a stop execution error, so just notifying is enough
  • Just log an error: this error should not stop the execution but you log it for future analysis

Conclusion

in this post, we’ve seen when a FileNotFoundException can occur and several options to handle it. if still, you have any doubts then feel free to drop your question in the comment section.

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers