Wednesday, 31 July 2013

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.