[英]Java is creating file in tomcat bin folder
I have written a java program in which I am creating a file. 我已经编写了一个在其中创建文件的Java程序。
BufferedWriter writer =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("myfile.txt"))
It is creating the file in the current folder (my project folder). 它正在当前文件夹(我的项目文件夹)中创建文件。 Till here everything is perfect. 直到这里一切都完美。 Now I have designed a servlet and call this class. 现在,我已经设计了一个servlet并调用该类。 The file is now getting created in the tomcat bin folder. 现在在tomcat bin文件夹中创建该文件。 Do I need to make any changes to classpath
or server.xml
or context.xml
in tomcat? 我需要对tomcat中的classpath
或server.xml
或context.xml
进行任何更改吗?
CAUSE: 原因:
Your program is creating a new file in the "current working directory". 您的程序正在“当前工作目录”中创建一个新文件。
Since it's a servlet running inside of Tomcat, the "current directory" happens to be Tomcat's "bin" directory. 由于它是在Tomcat内部运行的servlet,因此“当前目录”恰好是Tomcat的“ bin”目录。
SUGGESTIONS: 几点建议:
If possible, use a fully qualified path ( "/tmp/myfile.text"
) 如果可能,请使用完全限定的路径( "/tmp/myfile.text"
)
You can also write your file relative to getServletContext()
: 您还可以相对于getServletContext()
编写文件:
How to store a file on a server(web container) through a Java EE web application? 如何通过Java EE Web应用程序将文件存储在服务器(Web容器)上?
WEB-INF\\myprog.properties
) or JVM parameter ( -D filepath=/my/file/path
). 您可以使用相对路径,并在属性文件( WEB-INF\\myprog.properties
)或JVM参数( -D filepath=/my/file/path
)中对该路径进行参数化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.