This section illustrates you how to read text file in servlets.
In this example we will use the input stream to read the text from the disk file. The InputStreamReader class is used to read the file in servlets program. You can use this code in your application to read some information from a file.
Create a file message.properties in the
/WEB-INF/ directory. We will read the content of this file and
display in the browser.
Get the file InputStream using ServletContext.getResourceAsStream() method. If input stream is not equal to null, create the object of InputStreamReader and pass it to the BufferedReader. A variable text is defined of String type. Read the file line by line using the while loop ((text = reader.readLine()) != null). Then the writer.println(text) is used to display the content of the file
Here is the file message.properties which is going to be read through a
Here is the code of ReadTextFile.java
This will read the file and following output will be displayed on your browser
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.