Add to Google Reader or Homepage

Three reasons for java.io.FileNotFoundException

File not found Exception

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

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)

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("ExcelRead.xls"));
WritableSheet sheet=wbook12.createSheet("First Sheet",0);
Label l1=new Label(0,1,"Hello");
Label l2=new Label(0,2,"World");
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 "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).




15 comments:

Mina Guirguis said...

There is another reason if you need anministrative authority for this file it will give you the same error

here is my new blog for programming tutorials its still new so I am in for beginners phase but later you might find something useful there

Programming tutorials

Ganesh Bhuddhan said...

I too encountered this error due to administrative reasons, but when writing this post i forget to include that...anyway thanks for reminding me of this reason..

Mina Guirguis said...

No problem we are programming fellows :-) so have you thought about making another post with the way to solve those Exceptions or at least get an understandable error message for them?

arun j said...

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


class ExcepTest{
public static void main(String[] args)
{
int i=10/0;
System.out.println(i);
}
}

Netta Thompson said...

can anyone help with a small server issue - seems to be JAVA

---- Minecraft Crash Report ----
// Ooh. Shiny.

Time: 6/20/15 6:01 AM
Description: Exception in server tick loop

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:

Netta Thompson said...

part 2

-- System Details --
Details:
Minecraft Version: 1.7.10
Operating System: Windows 8.1 (amd64) version 6.3
Java Version: 1.7.0_67, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 1457720784 bytes (1390 MB) / 2225602560 bytes (2122 MB) up to 2863661056 bytes (2731 MB)
JVM Flags: 3 total; -Xmx3G -Xms2G -XX:MaxPermSize=256M
AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
FML: MCP v9.05 FML v7.10.99.99 Minecraft Forge 10.13.3.1420 76 mods loaded, 76 mods active
mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized
FML{7.10.99.99} [Forge Mod Loader] (server.jar) Unloaded->Constructed->Pre-initialized
Forge{10.13.3.1420} [Minecraft Forge] (server.jar) Unloaded->Constructed->Pre-initialized

Netta Thompson said...

part 3

appliedenergistics2-core{rv2-stable-5} [AppliedEnergistics2 Core] (minecraft.jar) Unloaded->Constructed->Pre-initialized
CodeChickenCore{1.0.7.46} [CodeChicken Core] (minecraft.jar) Unloaded->Constructed->Pre-initialized
ivtoolkit{IvToolkit} [1.2] (minecraft.jar) Unloaded->Constructed->Pre-initialized
NotEnoughItems{1.0.5.110} [Not Enough Items] (NotEnoughItems-1.7.10-1.0.5.110-universal.jar) Unloaded->Constructed->Pre-initialized
OpenComputers|Core{1.0.0} [OpenComputers (Core)] (minecraft.jar) Unloaded->Constructed->Pre-initialized
{000} [CoFH ASM] (minecraft.jar) Unloaded->Constructed->Pre-initialized
betterloadingscreen{1.3.0} [Better Loading Screen] ([1.7.10] BetterLoadingScreen-1.3.0.jar) Unloaded->Constructed->Pre-initialized
securitycraft{v1.7.4.1} [SecurityCraft] ([1.7.10] SecurityCraft v1.7.4.1.jar) Unloaded->Constructed->Pre-initialized
bspkrsCore{6.16} [bspkrsCore] ([1.7.10]bspkrsCore-universal-6.16.jar) Unloaded->Constructed->Pre-initialized
StartingInventory{1.7.10.r03} [StartingInventory] ([1.7.10]StartingInventory-universal-1.7.10.r03.jar) Unloaded->Constructed->Pre-initialized
Treecapitator{1.7.10} [Treecapitator] ([1.7.10]Treecapitator-universal-2.0.4.jar) Unloaded->Constructed->Pre-initialized
appliedenergistics2{rv2-stable-5} [Applied Energistics 2] (appliedenergistics2-rv2-stable-5.jar) Unloaded->Constructed->Pre-initialized
betterrain{0.15} [Better Rain] (betterrain-1.7.2-1.7.10_0.15.jar) Unloaded->Constructed->Pre-initialized
betterrecords{1.7.10-1.1.9} [Better Records] (betterrecords-1.7.10-1.1.9.jar) Unloaded->Constructed->Pre-initialized
betterstorage{0.13.1.126} [BetterStorage] (BetterStorage-1.7.10-0.13.1.126.jar) Unloaded->Constructed->Pre-initialized
BiomesOPlenty{2.1.0} [Biomes O' Plenty] (BiomesOPlenty-1.7.10-2.1.0.1283-universal.jar) Unloaded->Constructed->Pre-initialized
CarpentersBlocks{3.3.6} [Carpenter's Blocks] (Carpenter's Blocks v3.3.6 - MC 1.7.10.jar) Unloaded->Constructed->Pre-initialized
ForgeMultipart{1.1.2.331} [Forge Multipart] (ForgeMultipart-1.7.10-1.1.2.331-universal.jar) Unloaded->Constructed->Pre-initialized
chisel{2.3.9.36} [Chisel 2] (Chisel2-2.3.9.36.jar) Unloaded->Constructed->Pre-initialized

Netta Thompson said...

part 4
CoFHCore{1.7.10R3.0.2} [CoFH Core] (CoFHCore-[1.7.10]3.0.2-262.jar) Unloaded->Constructed->Pre-initialized
lootablebodies{1.3.5} [DrCyano's Lootable Bodies] (CyanosLootableBodies-1.3.5-backport.jar) Unloaded->Constructed->Pre-initialized
eplus{3.0.2-d} [Enchanting Plus] (EnchantingPlus-1.7.10-3.0.2-d.jar) Unloaded->Constructed->Pre-initialized
ThermalFoundation{1.7.10R1.0.0} [Thermal Foundation] (ThermalFoundation-[1.7.10]1.0.0-81.jar) Unloaded->Constructed->Pre-initialized
ExtraUtilities{1.2.5} [Extra Utilities] (extrautilities-1.2.5.jar) Unloaded->Constructed->Pre-initialized
farseek{1.0.8} [Farseek] (Farseek-1.0.8.jar) Unloaded->Constructed->Pre-initialized
FastCraft{1.21} [FastCraft] (fastcraft-1.21.jar) Unloaded->Constructed->Pre-initialized
flansmod{4.10.0} [Flan's Mod] (Flans Mod-1.7.10-4.10.0.jar) Unloaded->Constructed->Pre-initialized
iChunUtil{4.2.2} [iChunUtil] (iChunUtil-4.2.2.jar) Unloaded->Constructed->Pre-initialized
Hats{4.0.1} [Hats] (Hats-4.0.1.jar) Unloaded->Constructed->Pre-initialized
HatStand{4.0.0} [HatStand] (HatStand-4.0.0.jar) Unloaded->Constructed->Pre-initialized
HardcoreQuesting{The Journey (4.2.4)} [Hardcore Questing Mode] (hqm-rexxit-4.2.4.jar) Unloaded->Constructed->Pre-initialized
InventoryPets{1.1.2a} [Inventory Pets] (inventorypets-1.7.10-1.1.2a.jar) Unloaded->Constructed->Pre-initialized
inventorytweaks{1.59-dev-156-af3bc68} [Inventory Tweaks] (InventoryTweaks-1.59-dev-156.jar) Unloaded->Constructed->Pre-initialized
Waila{1.5.10} [Waila] (Waila-1.5.10_1.7.10.jar) Unloaded->Constructed->Pre-initialized
JABBA{1.2.1} [JABBA] (Jabba-1.2.1a_1.7.10.jar) Unloaded->Constructed->Pre-initialized
jcanimation{1.2.4} [JCAnimationAPI] (JurassiCraft-1.4.0bugfix.jar) Unloaded->Constructed->Pre-initialized
llibrary{0.2.0-1.7.10} [LLibrary] (LLibrary-0.2.0-1.7.10.jar) Unloaded->Constructed->Pre-initialized
jurassicraft{1.4.0} [JurassiCraft] (JurassiCraft-1.4.0bugfix.jar) Unloaded->Constructed->Errored
LunatriusCore{1.1.2.21} [LunatriusCore] (LunatriusCore-1.7.10-1.1.2.21-universal.jar) Unloaded->Constructed->Pre-initialized
malisiscore{1.7.10-0.12.2} [MalisisCore] (malisiscore-1.7.10-0.12.2.jar) Unloaded->Constructed->Pre-initialized
malisisdoors{1.7.10-1.8.1} [Malisis' Doors] (malisisdoors-1.7.10-1.8.1.jar) Unloaded->Constructed->Pre-initialized
Mantle{1.7.10-0.3.2.jenkins184} [Mantle] (Mantle-1.7.10-0.3.2.jar) Unloaded->Constructed->Pre-initialized
Mekanism{8.1.4} [Mekanism] (Mekanism-1.7.10-8.1.4.232.jar) Unloaded->Constructed->Pre-initialized
MekanismGenerators{8.1.4} [MekanismGenerators] (MekanismGenerators-1.7.10-8.1.4.232.jar) Unloaded->Constructed->Pre-initialized
MekanismTools{8.1.4} [MekanismTools] (MekanismTools-1.7.10-8.1.4.232.jar) Unloaded->Constructed->Pre-initialized
MineTweaker3{3.0.9B} [MineTweaker 3] (MineTweaker3-1.7.10-3.0.9C.jar) Unloaded->Constructed->Pre-initialized
MTRM{1.0} [MineTweakerRecipeMaker]

Netta Thompson said...

part 5
(MineTweakerRecipeMaker-1.7.10-1.1.0.11.jar) Unloaded->Constructed->Pre-initialized
numina{0.4.0.7-QMX} [Numina] (Numina-0.4.0.7-QMX.jar) Unloaded->Constructed->Pre-initialized
powersuits{0.11.0.7-QMX} [MachineMuse's Modular Powersuits] (ModularPowersuits-0.11.0.7-QMX.jar) Unloaded->Constructed->Pre-initialized
powersuitaddons{0.11.0.7-QMX} [Modular Powersuit Addons] (ModularPowersuitsAddons-0.11.0.7-QMX.jar) Unloaded->Constructed->Pre-initialized
NetherOres{1.7.10R2.3.0} [Nether Ores] (NetherOres-[1.7.10]2.3.0-12.jar) Unloaded->Constructed->Pre-initialized
ThermalExpansion{1.7.10R4.0.1} [Thermal Expansion] (ThermalExpansion-[1.7.10]4.0.1-182.jar) Unloaded->Constructed->Pre-initialized
OpenComputers{1.5.12.26} [OpenComputers] (OpenComputers-MC1.7.10-1.5.12.26-universal.jar) Unloaded->Constructed->Pre-initialized
harvestcraft{1.7.10i} [Pam's HarvestCraft] (Pam's HarvestCraft 1.7.10i.jar) Unloaded->Constructed->Pre-initialized
Ping{%MOD_VERSION} [Ping] (Ping-1.7.X-1.0.2.B6-universal.jar) Unloaded->Constructed->Pre-initialized
reccomplex{0.9.6.2} [Recurrent Complex] (RecurrentComplex-0.9.6.2.jar) Unloaded->Constructed->Pre-initialized
RedstonePasteMod{1.6.2} [Redstone Paste] (RedstonePasteMod-1.7.10-1.6.2.jar) Unloaded->Constructed->Pre-initialized
Schematica{1.7.5.120} [Schematica] (Schematica-1.7.10-1.7.5.120-universal.jar) Unloaded->Constructed->Pre-initialized
Stackie{1.6.0.35} [Stackie] (Stackie-1.7.10-1.6.0.35-universal.jar) Unloaded->Constructed->Pre-initialized
streams{0.1.4} [Streams] (Streams-0.1.4.jar) Unloaded->Constructed->Pre-initialized
supercraftingframe{1.7.10.3} [Super Crafting Frame] (supercraftingframe-1.7.10.3.jar) Unloaded->Constructed->Pre-initialized
Sync{4.0.0} [Sync] (Sync-4.0.0.jar) Unloaded->Constructed->Pre-initialized
TConstruct{1.7.10-1.8.5.build957} [Tinkers' Construct] (TConstruct-1.7.10-1.8.5.jar) Unloaded->Constructed->Pre-initialized
ThermalDynamics{1.7.10R1.0.0} [Thermal Dynamics] (ThermalDynamics-[1.7.10]1.0.0-122.jar) Unloaded->Constructed->Pre-initialized
TMechworks{0.2.14.100} [Tinkers' Mechworks] (TMechworks-1.7.10-0.2.14.100.jar) Unloaded->Constructed->Pre-initialized
tradeboothmod{1.7.10.1} [Trade Booth Mod] (tradebooth.1.7.10.1.jar) Unloaded->Constructed->Pre-initialized
WR-CBE|Core{1.4.1.9} [WR-CBE Core] (WR-CBE-1.7.10-1.4.1.9-universal.jar) Unloaded->Constructed->Pre-initialized
WR-CBE|Addons{1.4.1.9} [WR-CBE Addons] (WR-CBE-1.7.10-1.4.1.9-universal.jar) Unloaded->Constructed->Pre-initialized
WR-CBE|Logic{1.4.1.9} [WR-CBE Logic] (WR-CBE-1.7.10-1.4.1.9-universal.jar) Unloaded->Constructed->Pre-initialized
McMultipart{1.1.2.331} [Minecraft Multipart Plugin] (ForgeMultipart-1.7.10-1.1.2.331-universal.jar) Unloaded->Constructed->Pre-initialized
aobd{2.6.2} [Another One Bites The Dust] (AOBD-2.6.2.jar) Unloaded->Constructed->Pre-initialized
IguanaTweaksTConstruct{1.7.10-2.1.5.140} [Iguana Tinker Tweaks] (IguanaTinkerTweaks-1.7.10-2.1.5.jar) Unloaded->Constructed->Pre-initialized
ForgeMicroblock{1.1.2.331} [Forge Microblocks] (ForgeMultipart-1.7.10-1.1.2.331-universal.jar) Unloaded->Constructed->Pre-initialized
AE2 Version: stable rv2-stable-5 for Forge 10.13.2.1291
CoFHCore: -[1.7.10]3.0.2-262
ThermalFoundation: -[1.7.10]1.0.0-81
Mantle Environment: Environment healthy.
NetherOres: -[1.7.10]2.3.0-12
ThermalExpansion: -[1.7.10]4.0.1-182
TConstruct Environment: Environment healthy.
ThermalDynamics: -[1.7.10]1.0.0-122
Profiler Position: N/A (disabled)
Is Modded: Definitely; Server brand changed to 'fml,forge'
Type: Dedicated Server (map_server.txt)

Netta Thompson said...

sorry about the 5 parts, but I wanted all to be seen so my issue could be solved.

Niket Agrawal said...

what is the solution to reason 3 ,,, i am facing similar issue related to reason 3 .

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

حسام داود said...

شركة تسليك مجاري المطبخ بالرياض
شركة تسليك مجارى الحمام بالرياض
level تسليك المجاري بالرياض
افضل شركة تنظيف بالرياض
تنظيف شقق بالرياض
شركة تنظيف منازل بالرياض
شركة غسيل خزنات بالرياض
افضل شركة مكافحة حشرات بالرياض
رش مبيدات بالرياض
شركة تخزين عفش بالرياض
شركة تنظيف مجالس بالرياض
تنظيف فلل بالرياض
ابى شركة تنظيف بالرياض

Priya Kannan said...

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

sunitha vishnu said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top