Reputation: 227
in the tomcat6 server I have a project that contains a servlet,
I called in a servlet:
BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));
out.write("text text text");
out.close();
it creates the file in the directory of eclipse.exe cons I want them to create in the directory of the project.
how do
Upvotes: 0
Views: 2918
Reputation: 14877
In Servlet:
String path = getServletContext().getRealPath("/filename.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(path));
Note: It will work when you will deploy it in tomcat and run from out side eclipse. As eclipse has its own internal structure when it deploy the web application.
To check differnce put
System.out.println("File Path: " + path);
in your servlet.
Upvotes: 1