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?

Erik A. Brandstadmoen
  • 9,864
  • 2
  • 34
  • 52
Hyder
  • 11
  • 1
  • 2
  • 1
    Refer this http://stackoverflow.com/questions/19972243/accessing-linux-local-file-system-from-java-web-application – vishal patel Mar 10 '15 at 18:32

1 Answers1

2

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).
Community
  • 1
  • 1
FoggyDay
  • 10,517
  • 4
  • 24
  • 33