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:
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:
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:
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).
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.
Hi, I used this solution to generate code, but I have problem to use this method implementation in ocl validation. I got this stacktrace:
ReplyDeleteorg.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)