-3

ReadFile Class

public class  ReadFile {

    public void  readFile() throws IOException {

        BufferedReader is = new BufferedReader(new FileReader("D:\\text.txt"));

        if (is != null) {
            BufferedReader reader = new BufferedReader(is);
            String text;

            while ((text = reader.readLine()) != null) {

            }
            is.close();


        }
    }
    public static void  main(String[] args) throws IOException {

        ReadFile read=new ReadFile();
        read.readFile();
    }
}   

Servlet class

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{
    resp.setContentType("text/html");

    ReadFile readfile = new ReadFile();
    readfile.readFile();
}
AxelH
  • 13,322
  • 2
  • 21
  • 50
Shriya D.S
  • 47
  • 1
  • 6
  • Can you explain what is the problem ? It seems you read the file but don't do much with the line... might be the problem... – AxelH Apr 05 '18 at 11:52
  • You can refer https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java to read text file and also https://stackoverflow.com/questions/20593832/how-do-i-read-text-file-and-output-in-servlet – Unknown Apr 05 '18 at 12:03
  • @AxelH i have created one read class to read the contents of a file. and i have created a servlet class andi Im calling the read file .. my problem is the how to print the file in the browser? – Shriya D.S Apr 05 '18 at 12:11
  • if you have read the text file and then you are just to left print out the text into the response. – Shafin Mahmud Apr 05 '18 at 12:12
  • @ShafinMahmud but how to do it in the servlet class .. sorry im new to java and need to knw . please help me – Shriya D.S Apr 05 '18 at 12:14

1 Answers1

0

The class ReadFile needs to receive a PrintWriter parameter, resp.getWriter(). The character encoding was not handled both on reading the file, as on presenting it in the browser. Here I elected for both UTF-8.

For the file it might be Charset.forName("Windows-1252") or such.

As the java Files class has everything that ReadFile could do, I used that.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    Path path = Paths.get("D:\\text.txt");
    Files.lines(path, StandardCharsets.UTF_8)
        .forEach(out::println));
}

Or using HTML formatting:

    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    out.println("<!DOCTYPE html>");
    out.println("<html><head>");
    out.println("<meta charset='UTF-8'>");
    out.println("<title>The text<title>");
    out.println("</head><body><pre>");
    Path path = Paths.get("D:\\text.txt");
    Files.lines(path, StandardCharsets.UTF_8)
        .forEach(line -> {
             line = line.replace("&", "&amp;")
                 .replace("<", "&lt;")
                 .replace(">", "&gt;");
             out.printl(line);
        });
    out.println("</pre></body></html>");

With using a ReadFile class:

public class  ReadFile {

    public void  readFile(PrintWriter out) throws IOException {
        Path path = Paths.get("D:\\text.txt");
        Files.lines(path, StandardCharsets.UTF_8)
            .forEach(out::println));
    }

    public static void main(String[] args) throws IOException {   
        ReadFile read = new ReadFile();
        read.readFile(new PrintWriter(System.out));
    }
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    ReadFile read = new ReadFile();
    read.readFile(System.out);
}
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121