我想在JSP
页面中阅读并显示文本文件。我在代码中发现没有错误,但在运行它后显示空白。我有这样的代码:
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Read Text</title>
</head>
<body>
<%
String jspPath = "C:\\log\\";
String fileName = "log.txt";
String txtFilePath = jspPath + fileName;
BufferedReader reader = new BufferedReader(new FileReader(txtFilePath));
//BufferedReader br = new InputStreamReader(new FileInputStream(txtFilePath));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine())!= null){
sb.append(line+"\n");
}
System.out.println(sb.toString());
%>
</body>
</html>
答案 0 :(得分:1)
要发送包含对用户网络浏览器使用响应的文字:
out.println(sb.toString());
因为
System.out.println(sb.toString());
会将其发送到Tomcat的日志,而不是用户。