1

I have written a java program in which I am creating a file.

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. The file is now getting created in the tomcat bin folder. Do I need to make any changes to classpath or server.xml or context.xml in tomcat?

1

1 Answer 1

1

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.

SUGGESTIONS:

  • If possible, use a fully qualified path ("/tmp/myfile.text")

  • You can also write your file relative to getServletContext():

How to store a file on a server(web container) through a Java EE web application?

  • 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).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.