File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determining the location of the file

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In developing my web application i have used a file to register and search data
the is stored beside the class that do the operations of reading and writing
well, when i write the name of the file as a path for it as follow

the compiler flag FileNotFoundException why???
thanks...
 
Tim Moores
Bartender
Posts: 2502
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Web apps have no concept of a "default directory", so you can't use relative paths for files. You either need to use absolute paths, or -for directories inside your web app directory- use ServletContext.getRealPath(...).

Oh, now I see it's about the compiler error - "new File(...)" throws that exception, so you need to handle it properly (using a try/catch block).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getRealPath() won't work in all circumstances. It's best to use ServletContext.getResourceAsStream().
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new File(...) will never throw an exception.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this isn't servlet
it is normal class so the ServletContext will not work here
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the stream at the servlet level and pass it to the normal class.

There's no way that your "normal" class can determine the location of the file (unless the full path is hard-coded -- not a good idea) without the help of the servlet layer.

Or, place the file somewhere in the classpath and use ClassLoader.getResourceAsStream().

Or, store the absolute path of the file in a properties file that you read at run time.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the class
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a normal class why is it importing servlet classes?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, what's the purpose of reading the file in the first place?
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was trying to use ServletContext
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on what you are really trying to do the best solution is likely to not be moving servlet information into the class.

But you haven't answered my latest question yet.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this class will search in the file about the username and password specified
and if found it return a User object that will be put in the session later
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Fed wrote:this class will search in the file about the username and password specified
and if found it return a User object that will be put in the session later

What's in the file that needs to be looked up? Is this not something better handled by a database?
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but i still doesn't have the skills to deal with database
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion you are spending a lot of time trying to do things the wrong way, and that time would be better spent learning the skills you lack to do it the right away.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just using this for a learning experience (your reasoning for not wanting to involve a database), you may want to consider making the username / password either servlet init parameters, or context parameters for the web application. DO NOT DO THIS WITH A SECURE PASSWORD, but if you want to use it just for testing, this might be a good approach.

Bear's right. For an actual enterprise solution, a database (or LDAP server) is a better option.



 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic