File not found Exception

There are three reason,for this java.io.FileNotFoundException to be thrown at run-timeand 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:/exam.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:\exam.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.(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".

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)

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".

Here what am i trying to do is to write some labels in to the "ExcelRead.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:\blog>javac ExcelRead.java

C:\blog>java ExcelRead
Exception in thread "main" java.io.FileNotFoundException: ExcelRead.xls (The pro
cess 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).

If the FileNotFoundException thrown at Runtime, then why we need to write throws Excepeion at compiletime .

For RuntimeExceptions , we will not write throws Exceptions right.. It will compile .But when there is chance to raise FileNotFoundException that time why we writing throws Exception at compiletime..please clarify the doubt

cpw.mods.fml.common.LoaderException: java.io.FileNotFoundException: http://pastebin.com/PQgLKE5F at cpw.mods.fml.common.LoadController.transition(LoadController.java:163) at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:538) at cpw.mods.fml.server.FMLServerHandler.beginServerLoading(FMLServerHandler.java:88) at cpw.mods.fml.common.FMLCommonHandler.onServerStart(FMLCommonHandler.java:314) at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:117) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:387) at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:685)Caused by: java.io.FileNotFoundException: http://pastebin.com/PQgLKE5F at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.URL.openStream(Unknown Source) at net.ilexiconn.llibrary.common.web.WebHelper.downloadTextFile(WebHelper.java:80) at net.ilexiconn.llibrary.common.web.WebHelper.readPastebin(WebHelper.java:43) at net.ilexiconn.jurassicraft.JurassiCraft.init(JurassiCraft.java:83) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:532) at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:212) at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:190) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119) at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:535) ... 5 more

A detailed walkthrough of the error, its code path and all known details is as follows:

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. Java Training in Chennai