Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Write to a temporary file

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Write to a temporary file package com.ack.web.servlet; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WriteToATemporaryFile extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { // get the web applications temporary directory File tempDir = (File) getServletContext(). getAttribute( "javax.servlet.context.tempdir" ); // create a temporary file in that directory File tempFile = File.createTempFile( getServletName(), ".tmp", tempDir ); // write to file FileWriter fw = new FileWriter( tempFile ); try { fw.write( "done and dusted" ); } finally { fw.close(); } // tell servlet client where to look for file res.getWriter().println( "check file: " + tempFile.getAbsolutePath() ); } }