Zombie_Colonel
Zombie_Colonel

Reputation: 227

How creating file in the directory of the project

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

Answers (1)

Hardik Mishra
Hardik Mishra

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

Related Questions