Tuesday, February 8, 2011

Writing A text/xml file into a Tomcat WEB-INF folder

How to write a file(text or xml) file into a TOMCAT WEB-INF folder


code snippet  for writing a text file into the Tomcat WEB-INF folder:

...
...
String realPath = getServletContext().getRealPath("/WEB-INF/log");
// it returns  D:\apps\apache-tomcat-6.0.24\webapps\CodeAnalyser\WEB-INF\log  as realPath value

File curFile = File.createTempFile("SopOut", ".txt", new File(realPath));

// it will creat a file SopOUt.txt in to the  D:\apps\apache-tomcat-6.0.24\webapps\CodeAnalyser\WEB-INF\log  folder

// write to file
FileWriter fw = new FileWriter(curFile);

try {
      fw.write(sb); // sb contains the content to be written into the file
} finally {
    fw.flush();
    fw.close();
}

...
...

No comments:

Post a Comment