How can I read a text file from JSP page?

Mauricio Giraldo

Here is some simple code I wrote to read a file and then write it in a different place in the same server:

<%@page <%@page language="java" import="java.io.*"%>
// Remember to declare the path correctly:
// Windows: path_to_fileFile.txt
// Unix: path_to_file/file.txt
// First: Read the file
FileInputStream data = new FileInputStream("path/file.txt");
BufferedInputStream file = new BufferedInputStream(data);
// Second: Write the file line by line
OutputStream out = new FileOutputStream("path/file2.txt");
int c = -1;
while((c = file.read()) != -1) {
// Close and flush everything

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.