简体   繁体   English

Java正在tomcat bin文件夹中创建文件

[英]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中的classpathserver.xmlcontext.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容器)上?

  • You can use a relative path, and parameterize either that path in a properties file ( 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.

相关问题 获取java.util.MissingResourceException的Tomcat \\ bin文件夹中的属性文件 - Property file in Tomcat\bin folder getting java.util.MissingResourceException 下载文件显示在 tomcat bin 文件夹中 - Download file showing in tomcat bin folder Apache Tomcat-使文件可从bin文件夹下载 - Apache Tomcat - Make file downloadable from bin folder 直接在 tomcat 文件夹中创建图像存储(Java) - Creating a storage for images directly in tomcat folder (Java) Java Web应用程序的bin文件夹中的访问文件 - Access File in bin folder of Java web application Java获取我的项目/ bin /文件夹中文件的路径? - Java get the path of a file in my project /bin/ folder? 如何读取位于eclipse中java项目文件夹中的bin文件 - How to read bin file located in java project's folder in eclipse 经过xslt转换后,为什么在Tomcat / bin文件夹中生成文件? 如何将此目录更改为webapps / myApp? - After xslt transformation why file is generated in Tomcat/bin folder ? How can i change this directory to webapps/myApp? 为什么新的File()期望文件位于Java Web应用程序的TOMCAT_HOME / bin中? - Why new File() expects file to be in TOMCAT_HOME/bin in Java web app? Java - 在Web服务器目录中创建文件夹和文件 - Java - creating a folder and file in web server directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM