Skip to main content

FileNotFoundException in Java


There are three  reason,for this java.io.FileNotFoundException to be thrown at run-time and the reasons are follows:

Reason1: 
 

"If the given file is not available in the specified location then this error will occur".
As you see,this is the most obvious reason for this exception as indicated by the Exception itself.
Example:

import java.io.*; public class Example1{ public static void main(String[] args){ FileReader reader = new FileReader("c:/javaInterviewExam.txt"); BufferedReader br = new BufferedReader(reader); String strcontent =null; while ((strcontent = br.readLine()) != null){ System.out.println(strcontent); } br.close(); } }

In the above code during the execution of the line BufferedReader br=new BufferedReader(reader); if the file exam.txt is not found in the given location then the following error message will appear.
Error message:

java.io.FileNotFoundException: c:\javaInterviewExam.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(Unknown Source)        
        at java.io.FileInputStream.open(Native Method)        at java.io.FileInputStream.(Unknown Source)                at java.io.FileInputStream.(Unknown Source)                at java.io.FileReader.(Unknown Source)                at Example1.main(Example1.java:9)

This exception can be resolved by giving the correct path of the file or storing the file in the given location.

Reason 2:
"If the given file is inaccessible, say for an example if it is write protected(i.e, ReadOnly) then you can able to read the file but when you try to modify(write into) the file then this error will occur".
Example: 

import java.util.*; import java.io.*; class ReadingFiles{ public static void main(String args[]){ try{ File f=new File("FileOut1.txt"); PrintWriter p=new PrintWriter(new FileWriter(f),true); p.println("Hello world"); p.println("HI PEOPLE"); p.println("hello"); p.close(); f.setReadOnly(); PrintWriter p1=new PrintWriter(new FileWriter("FileOut1.txt"),true); p1.println("World"); }catch(Exception e){ e.printStackTrace(); } } } ERROR MESSAGE:
java.io.FileNotFoundException: FileOut1.txt (Access is denied)        at java.io.FileOutputStream.open(Native Method)        at java.io.FileOutputStream.(Unknown Source)
        at java.io.FileOutputStream.(Unknown Source)
        at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at java.io.FileOutputStream.(Unknown Source)
        at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at ReadingFiles.main(ReadingFiles.java:13)                at java.io.FileOutputStream.(Unknown Source)
        at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at ReadingFiles.main(ReadingFiles.java:13)                at java.io.FileWriter.(Unknown Source)
        at ReadingFiles.main(ReadingFiles.java:13)
        at ReadingFiles.main(ReadingFiles.java:13)                at ReadingFiles.main(ReadingFiles.java:13)

If you see the statements in BOLD in the above program you can able to understand that after setting the readOnly() flag of that file i have tried to write into that file "again". Thus this exception is thrown saying that "Access is Denied". So an important thing to be noted here is if you try to write into a read only file then this exception will be thrown.

Reason 3:

"In some cases, if the file that you are trying to access for read/write operation  is opened by another program then this error will occur".
Example:

import java.io.*; import jxl.*; import jxl.Workbook; import jxl.write.*; public class ExcelRead{ public  static void main(String args[]) throws Exception{ Cell b[]=new Cell[2]; WritableWorkbook wbook12=Workbook.createWorkbook( new File("ReadExcelFile.xls")); WritableSheet sheet=wbook12.createSheet("First Sheet",0); Label l1=new Label(0,1,"Hello"); Label l2=new Label(0,2,"World how are you ?"); sheet.addCell(l1); sheet.addCell(l2); wbook12.write(); wbook12.close(); } }

Here what am I trying to do is to write some labels in to the "ReadExcelFile.xls" file. I thought that this would work fine. But when I execute this program I got this "FileNotFoundException". Later i found that it (ExcelRead.xls) has been opened in MicrosoftExcel Application. So in some cases this kind of errors do occur.
C:\harikrushnavblog>javac ExcelRead.java
C:\harikrushnavblog>java ExcelRead

Exception in thread "main" java.io.FileNotFoundException: ReadExcelFile.xls (The process cannot access the file because it is being used by another process)        at java.io.FileOutputStream.open(Native Method)        at java.io.FileOutputStream.(Unknown Source)
        at java.io.FileOutputStream.(Unknown Source)
        at jxl.Workbook.createWorkbook(Workbook.java:301)
        at jxl.Workbook.createWorkbook(Workbook.java:286)
        at ExcelRead.main(ExcelRead.java:11).
        at java.io.FileOutputStream.(Unknown Source)
        at jxl.Workbook.createWorkbook(Workbook.java:301)
        at jxl.Workbook.createWorkbook(Workbook.java:286)
        at ExcelRead.main(ExcelRead.java:11).
        at jxl.Workbook.createWorkbook(Workbook.java:301)        at jxl.Workbook.createWorkbook(Workbook.java:286)        at ExcelRead.main(ExcelRead.java:11).                at java.io.FileOutputStream.(Unknown Source)
        at jxl.Workbook.createWorkbook(Workbook.java:301)
        at jxl.Workbook.createWorkbook(Workbook.java:286)
        at ExcelRead.main(ExcelRead.java:11).
        at jxl.Workbook.createWorkbook(Workbook.java:301)        at jxl.Workbook.createWorkbook(Workbook.java:286)        at ExcelRead.main(ExcelRead.java:11).                at jxl.Workbook.createWorkbook(Workbook.java:301)                at jxl.Workbook.createWorkbook(Workbook.java:286)                at ExcelRead.main(ExcelRead.java:11).


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


This exception will be thrown by the FileInputStreamFileOutputStream, 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.


Comments

  1. Hi, I used this solution to generate code, but I have problem to use this method implementation in ocl validation. I got this stacktrace:

    org.eclipse.ocl.common.internal.delegate.OCLDelegateException: OCL validation result of 'Approval::Test' is invalid for 'Class Approval'
    at org.eclipse.ocl.examples.pivot.delegate.OCLValidationDelegate.validateExpressionInOCL(OCLValidationDelegate.java:293)

    ReplyDelete

Post a Comment

Popular posts from this blog

HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException

HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException If you get following error while connecting to your Database with Spring boot, try the suggested solution below: 2018 - 11 - 25 15 : 28 : 50.078 INFO 5335 --- [ main] o. h . e . j . e . i . LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java. lang . reflect . InvocationTargetException java. lang . reflect . InvocationTargetException : null at sun. reflect . NativeMethodAccessorImpl . invoke0 (Native Method) ~[na: 1.8 . 0 _161] at sun. reflect . NativeMethodAccessorImpl . invoke (NativeMethodAccessorImpl. java : 62 ) ~[na: 1.8 . 0 _161] at sun. reflect . DelegatingMethodAccessorImpl . invoke (DelegatingMethodAccessorImpl. java : 43 ) ~[na: 1.8 . 0 _161] at java. lang . reflect . Method . invoke (Method. java : 498 ) ~[na: 1.8 . 0 _161] at org. hibernate . engin

Could not launch the product because the associated workspace is currently in use by another Eclipse application

If you are seeing following error at the beginning of the Eclipse or any RCP based product then here is the solution: Error Message: "Could not launch the product because the associated workspace is currently in use by another Eclipse application." or “Workspace in use or cannot be created chose a different one.” Solution :  go to workspace location remove file <workspace location>/.metadata/.lock Reason: Through eclipse, user can create multiple Workspace locations. One Workspace location can be edited by one user at a time. A workspace contains the data related to various project configuration and local file storage. To avoid multiple users to access the same workspace, eclipse is maintaining a ".lock" file inside the Workspace. Eclipse will remove this ".lock" file when you will close the Eclipse. But in some cases, the Eclipse will crash due to some error, due to which it could not able to remove ".lock" file from

How to resolve : print() on closed filehandle in perl

How to resolve : print() on closed filehandle in perl Error "print() on closed filehandle" generated in the following condition ( it is on of possible cause of it) -------------------------- BEGIN Code --------------------------------------- my $inputFile = '/this/is/path/of/your/input/file.txt'; open(MYINPUTFILE, ">","$inputFile"); print MYINPUTFILE "any data"; close(MYINPUTFILE); -------------------------- END Code --------------------------------------- here if you don't have permission to access the path '/this/is/path/of/your/input/file.txt' or the path (of parent directory) does not exist then it will throuw the error : print() on closed filehandle To resolve this better to make practice as follow while opening the file: -------------------------- BEGIN Code --------------------------------------- open(MYINPUTFILE, ">","$inputFile") or die "ERROR ::: unable access the file&