How to write data to an XML file present under application server
frnds: can ne one tell me, how to write data in to a file, which is present under a application server
Ex: i want to write a string data in to a file test.txt which is present under "http://localhost:8080/<some_webapp>/test.txt"
Note:i have deploted a service<some_webapp> under Tomcat/webapps dir
# 1
Very simple. A servlet can writes to that file if it has the good rights.
In the servlet get (or post) method, use a code like this:
import java.io.*;
....
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
String filePath = this.getServletContext().getRealPath("/text.txt");//See http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
PrintWriter writer = new PrintWriter (filePath);
//or BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
writer .println("aString");
writer.flush();
writer .close();
} catch (Exception e) {
e.printStackTrace();
}
}
Hope That Helps
# 2
Yes what you r telling is right. But i dont want to use servlets. I just want to create an file object (or something similar to this) which is pointing to the file under
tomcat/webaaps/<some_webapp>/<text file>
and use outputstream to write data in to the file.
can i do this?