首页 新闻 会员 周边

学习Java的I/O,为何print可全诗输出,页面只能显示两句?

0
悬赏园豆:5 [已解决问题] 解决于 2020-02-11 13:51
复制代码
import java.io.*;

@RestController
public class hello {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String  hello() throws IOException {

        /*
         * I/0 demo
         *读取本地text.txt文件,在页面显示
         */

        String result = null;

        //指定读取的文件
        File f = new File("text.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                result = line;
            }

            System.out.flush();

            br.close();
            isr.close();

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
复制代码

 

 

 

 

代码可以优化吗?

Coca-code的主页 Coca-code | 初学一级 | 园豆:10
提问于:2020-02-11 12:41
< >
分享
最佳答案
0

System.out.println(line);
result = line;

=====>
System.out.println(line);
result += line;

收获园豆:5
czd890 | 专家六级 |园豆:14412 | 2020-02-11 13:16

显示是显示了。

Coca-code | 园豆:10 (初学一级) | 2020-02-11 13:52
其他回答(1)
0

没见过这么写的, 你从哪向页面输出的...

风中的雪糕 | 园豆:418 (菜鸟二级) | 2020-02-11 13:27

从项目root

代码有的自己写,有的参考的。

现在解决了,但需优化...

支持(0) 反对(0) Coca-code | 园豆:10 (初学一级) | 2020-02-11 13:50
package com.progress.progress;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@RestController
public class hello {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String  hello() throws IOException {

        /*
         * I/0 demo
         * 
         */

        String result = "";

        //指定读取的文件
        File f = new File("text.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                result += line + "<br>";
            }

            br.close();
            isr.close();

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
支持(0) 反对(0) Coca-code | 园豆:10 (初学一级) | 2020-02-11 13:51

@Coca-code: 除了字符串加 改成stringbuilder, 其他可以了

支持(1) 反对(0) 风中的雪糕 | 园豆:418 (菜鸟二级) | 2020-02-11 16:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册