JSP Open File

JSP Open File is used to create a file and write a text into the file.
Understand with Example
The Tutorial illustrate an example from JSP Open File. To understand
the example we create openFile.jsp that include a page import directive "java.io"
package. The java.io package provides for system input and output through
data streams, serialization and the file system. For this, we have create a file 'Hello.txt' in the C:drive. The class FileOutputStream
opens an output stream in order to write the specified text of line using the
class PrintStream. In case the exception occurred , the catch block
handle the exception and print the 'Unable to write to file'.
Here is the code of openFile.jsp
<%@ page import="java.io.*"%>
<%
FileOutputStream out;
try{
out = new FileOutputStream("C://Hello.txt");
new PrintStream(out).println ("All glitters are not gold");
out.close();
}
catch (IOException e){
out.println ("Unable to write to file");
}
%> |
When you run the above jsp code, the line of text, you have specified, will
be written into the file Hello.txt.
Download Source Code:

|