In the file named test.txt, I have a list of Names as content. For example, let's take "Sachin Tendulkar" where there are four spaces between the first and last name. My servlet is responsible for reading this text file and displaying the names exactly as they appear in the file on the browser. Below is the servlet code I have written for this purpose.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
try
{
FileReader reader = new FileReader("E:/test.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
PrintWriter pw = response.getWriter();
String line = null;
while( (line = bufferedReader.readLine()) !=null)
{
pw.write(line);
}
pw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Result displayed on the browser: Sachin Tendulkar
However, the result does not show all 4 spaces between Sachin and Tendulkar. It appears that when there are multiple spaces in the file, the printwriter in the servlet condenses them into a single space. This is causing issues with our report feature that requires preserving all characters from the text file. Please help me find a solution to retain all spaces as shown in the sample above.