How do I read text file and output in Servlet?

后端 未结 2 475
孤城傲影
孤城傲影 2021-01-29 10:00

i have file: input.txt I want to read this file, put values in new output.txt from input.txt.

Servlet.java

protected void processRequest(HttpServletRequ         


        
2条回答
  •  感情败类
    2021-01-29 10:55

    Apache FileUtils, could make it simple

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        PrintWriter out = response.getWriter();
    
        List lines = FileUtils.readLines(new File("file.txt), "UTF-8");
    
        for (String line : lines) {
            out.println(line);
        }
    }
    

提交回复
热议问题