简体   繁体   English

如何参考正确的目录?

[英]How do I refer to the right Directory?

If my Java Class is in: ` 如果我的Java类位于:

package org.gamehost.flyingame.BookManagementServlet.ModelClass 包org.gamehost.flyingame.BookManagementServlet.ModelClass

aka: 又名:

BookManagementSoftwareServlet/org/gamehost/flyingame/BookManagementServlet/ModelClass 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: 在该类中,如果我想从目录中返回文件,请在执行此操作后说一个html文件:

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

Assuming the above code will create a new file, will it create it in the 假设上面的代码将创建一个新文件,它将在

BookManagementSoftwareServlet 图书管理软件Servlet

directory or, will it create it in this directory: 目录,或者它将在此目录中创建它:

BookManagementSoftwareServlet/org/gamehost/flyingame/BookManagementServlet/ModelClass 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 C:// Workspace / BookManagementSoftwareServlet / org / gamehost / flyingame / BookManagementServlet / ModelClass

Probably neither. 大概都不行。 It will go to the current user directory, unless the container has modified this behavior. 除非容器已修改此行为,否则它将转到当前用户目录。 The java.io.File javadoc says: java.io.File javadoc说:

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. 默认情况下,java.io包中的类始终针对当前用户目录解析相对路径名。 This directory is named by the system property user.dir , and is typically the directory in which the Java virtual machine was invoked. 该目录由系统属性user.dir命名,通常是在其中调用Java虚拟机的目录。

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 . 您没有提到您的容器,但这是有关Tomcat 6和安全管理器的一些信息。

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 . 如果这些文件是临时文件并已重新生成,则可以在从2.5 servlet规范的SRV.4.7.1临时工作目录中定义的javax.servlet.context.tempdir servlet上下文属性返回的绝对路径中创建它们。

A temporary storage directory is required for each servlet context. 每个Servlet上下文都需要一个临时存储目录。 Servlet containers must provide a private temporary directory for each servlet context, and make it available via the javax.servlet.context.tempdir context attribute. Servlet容器必须为每个Servlet上下文提供一个专用的临时目录,并通过javax.servlet.context.tempdir上下文属性使其可用。 The objects associated with the attribute must be of type java.io.File. 与该属性关联的对象必须是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. 我发现这样做可以减少阅读代码,查看堆栈跟踪等时的混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM