views:

60

answers:

1

If my Java Class is in: `

package org.gamehost.flyingame.BookManagementServlet.ModelClass

aka:

BookManagementSoftwareServlet/org/gamehost/flyingame/BookManagementServlet/ModelClass

and in that class, if I want to return a file from a directory, say an html file, after doing this:

File newFile = new File("HTMLFile.html");

Assuming the above code will create a new file, will it create it in the

BookManagementSoftwareServlet

directory or, will it create it in this directory:

BookManagementSoftwareServlet/org/gamehost/flyingame/BookManagementServlet/ModelClass

if no, should I have to then go the hard way by doing this:

C://Workspace/BookManagementSoftwareServlet/org/gamehost/flyingame/BookManagementServlet/ModelClass

+1  A: 

Probably neither. It will go to the current user directory, unless the container has modified this behavior. The java.io.File javadoc says:

A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

Containers often lock down file creation, so you might get a security error. You didn't mention your container, but here's some information on Tomcat 6 & Security Managers.

If these files are temporary & regenerated, you could create them in the absolute path returned from the javax.servlet.context.tempdir servlet context attribute defined in SRV.4.7.1 Temporary Working Directories of the 2.5 servlet spec.

A temporary storage directory is required for each servlet context. Servlet containers must provide a private temporary directory for each servlet context, and make it available via the javax.servlet.context.tempdir context attribute. The objects associated with the attribute must be of type java.io.File.

A useless style comment: I've always found it best that you keep package names all lowercase and use camel case for the class name. I've found this leads to less confusion when reading code, looking at stack traces, etc.

Wayne Young