Java 如何在 Servlet 中读取文本文件和输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20593832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:59:37  来源:igfitidea点击:

How do I read text file and output in Servlet?

javaservlets

提问by olle

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

我有文件:input.txt 我想读取这个文件,将值从 input.txt 放入新的 output.txt 中。

Servlet.java

小程序

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/plain");
    response.setHeader("Content-Disposition",
                 "attachment;filename=output.txt");
    PrintWriter out = response.getWriter();
    ServletContext cntxt = this.getServletContext();
    String fName = "/input.txt";
    InputStream ins = cntxt.getResourceAsStream(fName);
    try {
        if(ins != null){
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader reader = new BufferedReader(isr);
        int n = 0;
        String word ="";
        while((word= reader.readLine())!= null)
         {
             n = Integer.parseInt(word);
             out.println(n);
         }
      } finally {
            out.close();
      }
}

but output.txt is empty. What's wrong?

但是 output.txt 是空的。怎么了?

回答by Shamse Alam

Try following code

试试下面的代码

input.txtshould be present in the root directory of your application

input.txt应该存在于应用程序的根目录中

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=output.txt");
        PrintWriter out = response.getWriter();
        ServletContext cntxt = this.getServletContext();
        String fName = "/input.txt";
        InputStream ins = cntxt.getResourceAsStream(fName);
        try {
            if (ins != null) {
                InputStreamReader isr = new InputStreamReader(ins);
                BufferedReader reader = new BufferedReader(isr);
                int n = 0;
                String word = "";
                while ((word = reader.readLine()) != null) {
                    n = Integer.parseInt(word);
                    out.println(n);
                }
            }
        }finally {
            out.close();
        }
    }

回答by Jay

Apache FileUtils, could make it simple

Apache FileUtils,可以让它变得简单

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    List<String> lines = FileUtils.readLines(new File("file.txt), "UTF-8");

    for (String line : lines) {
        out.println(line);
    }
}