• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with java.io.FileNotFoundException  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I am confused and hope someone can help

This has to do with struts 2 action writing a file

I have the following code in this action



and I am getting this error

java.io.FileNotFoundException: \emp\Davie.emp (The system cannot find the path specified)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(Unknown Source)
java.io.FileOutputStream.<init>(Unknown Source)

I use First name to be the name of the file. The class file is in /WEB-INF/classes/Pay_Roll/Action/
what I am confused about isn't FileOutPutStream suppose to create Davie.emp in /WEB-INF/classes/Pay_Roll/Action/emp/
why is the system trying to find it?

Can anyone give me a starting point to look for solutions?

Thanks, appreciated
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's supposed to create the file not the directory. So if you don't have the directory, you'll have the exception.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's supposed to create the file not the directory. So if you don't have the directory, you'll have the exception.


Thanks, so the directory is missing, now I am not sure if I have the structure right or not

The class file is in /WEB-INF/classes/Pay_Roll/Action/ and I have made directory of
/WEB-INF/classes/Pay_Roll/Action/emp/

I am expecting Davie.emp to be in the directory above, is that correct?
since I use



this would leave Davie.emp (File based upon what I enter for First name) in /WEB-INF/classes/Pay_Roll/Action/emp/
is this correct?

am I doing something wrong? please give me a hint
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing something wrong? Well, yes. You are looking for the file in the /emp directory. But you say yourself that it's actually in the /WEB-INF/classes/Pay_Roll/Action/emp directory. Which is really the /something/something/WEB-INF/classes/Pay_Roll/Action/emp directory when you take into the account that it's in the server installation. Not surprising that you don't find it, then.

There are methods in the ServletContext class (I think) with names like "getRealPath". You should use them to find the actual path of the file you plan to create.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Paul, I have the wrong assumption, I thought



would create the file Davie.emp for me if I have the /WEB-INF/classes/Pay_Roll/Action/emp/ directory structure
without the /something/something/

anyways, should I supply /something/something/ or the absolute path for FileOutputStream for it to not throw the exception?
just want to make one thing clear, I thought the FileOutputStream will create a "Davie.emp" in /WEB-INF/classes/Pay_Roll/Action/emp/
not looking for "Davie.emp" in the directory above. That's what i was confused about,

I also look up the definition of FileNotFoundException it states
if the file exists but is a directory rather than a regular file


I don't understand what it mean by "if the file exist but is a directory"
If someone can kindly explain the quote above, I would really appreciated

Thank you
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Paul said, you need to use ServletContext's getRealPath method (I'm using / out of laziness; also because it works both in Linux and Windows):
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davie Lin wrote:
I also look up the definition of FileNotFoundException it states
if the file exists but is a directory rather than a regular file


I don't understand what it mean by "if the file exist but is a directory"
If someone can kindly explain the quote above, I would really appreciated


Say you have a directory [/users/somename/status], you create a File object with that path (i.e. is a directory) so file.exists() and file.isDirectory() are both true. However if you try to write to that file object (i.e. using FileOutputStream) then you will still have FileNotFoundException, because there is no such file just a directory.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have expected a different type of IOException, but you are completely right. The exception message will be "XXX (Access is denied)", where XXX is the file.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks guys, so much input
I will go thru everyone of them carefully

Wow, what would I do without this site,
Cool, I am gonna give all the option here a try, Thanks Bartender for
such nice code example, thank you so much, I will go from there, that's awesome

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!