How can I write to a text file from a JSP page?
Created May 4, 2012
anil chakravarthy You can write to a text file using the PrintWriter object:
<%@ page import="java.io.*" %> <% String str = "print me"; //always give the path from root. This way it almost always works. String nameOfTextFile = "/usr/anil/imp.txt"; try { PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); pw.println(str); //clean up pw.close(); } catch(IOException e) { out.println(e.getMessage()); } %>
Now, open up imp.txt and voila, the text "print me" should be there in it.