txt文件内容未显示在加载页面中

时间:2016-05-22 09:39:28

标签: jsf

我写了这个代码用于从文件中读取,当页面加载时,它显示在primefaces.But inputTextArea中没有显示。

我的代码:

    @Named("GlassfishLogFileController")
@SessionScoped
public class PrintReport implements Serializable {

    private static String logFile = null;

    public String logFile() {
        return logFile;
    }

    @PostConstruct
    public void init() {
        try {
            logFile = readLogFile();
        } catch (Exception e) {

        }
    }

    public String readLogFile() throws FileNotFoundException, IOException {
        BufferedReader br = null;
        br = new BufferedReader(new FileReader("D:\\Report.log"));

        //One way of reading the file
        System.out.println("Reading the file using readLine() method:");
        String contentLine = br.readLine();
        while (contentLine != null) {
            System.out.println(contentLine);
            contentLine = br.readLine();

        }
        return contentLine;
    }
}

XHTML:

 <h:inputTextarea rows="30" cols="1000" value="#{GlassfishLogFileController.logFile}" /> 

1 个答案:

答案 0 :(得分:1)

正如@Jaqen H&#39; ghar在评论中提到的,你应该创建一个getter方法来访问你的UI中的logFile。

public String getLogFile(){
        return logFile;
    }

更改readLogFile方法:

public String readLogFile() throws FileNotFoundException,IOException{
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("C:\\DefaultServer-diagnostic.log"));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line!=null) {
                sb.append(line);
                sb.append("\r\n");
                line = br.readLine();
            }
            return sb.toString();
        }
        finally {
            br.close();
        }
    }

了解更多信息Reading a plain text file in Java