在下面的代码中,我试图在JSP的文本区域中读取文件内容,它显示内容但是对于每行创建新的文本区域。我需要在一个文本区域中打印整个内容。这是代码
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.IOException"%>
<%
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\windows\\Documents\\NetBeansProjects\\Pricing\\build\\web\\image\\" + s + ""));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
%>
<textarea rows="10" cols="25"><%=sCurrentLine%></textarea>
<%
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
%>
答案 0 :(得分:2)
你的'textarea'必须在while循环之外。然后,您必须将结果附加到先前的sCurrentLine。像这样:
while ((sCurrentLine += br.readLine()) != null) {
答案 1 :(得分:0)
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.IOException"%>
<%
BufferedReader br = null;
String myString = "";
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\windows\\Documents\\NetBeansProjects\\Pricing\\build\\web\\image\\"+s+""));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
myString += sCurrentLine;
}
%>
<textarea rows="10" cols="25"><%=myString%></textarea>
<%
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
%>