jsp 读取一个txt文件(乱序的)文本内容,并且分页显示出来
例如下面的代码,从file.txt中读取了一行显示<%Stringpath=request.getRealPath("/0work");//取得目录在服务器端的实际位置F...
例如下面的代码,从file.txt中读取了一行显示
<%
String path = request.getRealPath("/0work"); //取得目录在服务器端的实际位置
FileReader fr = new FileReader(path + "\\File.txt"); //建立FileReader对象,并设定由fr对象变量引用
BufferedReader br = new BufferedReader(fr); //建立BufferedReader对象,并设定由br对象变量引
String Line = br.readLine(); //读取一行数据
out.println(Line + "<BR>"); //输出读取得的数据
br.close(); //关闭BufferedReader对象
fr.close(); //关闭文件
%>
现在的问题是假定这个file.txt共有有n行的内容,怎样使它一页显示100行并且分页显示,可以查找页面(不读取行数,读取字符数也可以啊) 求高人指点! 展开
<%
String path = request.getRealPath("/0work"); //取得目录在服务器端的实际位置
FileReader fr = new FileReader(path + "\\File.txt"); //建立FileReader对象,并设定由fr对象变量引用
BufferedReader br = new BufferedReader(fr); //建立BufferedReader对象,并设定由br对象变量引
String Line = br.readLine(); //读取一行数据
out.println(Line + "<BR>"); //输出读取得的数据
br.close(); //关闭BufferedReader对象
fr.close(); //关闭文件
%>
现在的问题是假定这个file.txt共有有n行的内容,怎样使它一页显示100行并且分页显示,可以查找页面(不读取行数,读取字符数也可以啊) 求高人指点! 展开
3个回答
2015-07-10
展开全部
参考实例如下:
BufferedReader bfR = null;
ArrayList list = new ArrayList();
try
{
String str = null;
bfR = new BufferedReader(new InputStreamReader(new FileInputStream(SortFile+FileName), "GB2312"));
while ((str = bfR.readLine()) != null)
{
number++;
String tempTrack = str.trim();
String[] arrVal = new String[3];
arrVal = tempTrack.split("/|");
if (arrVal.length < 3)
continue;
String order_no = arrVal[0]; //排名
String member_name = arrVal[1]; //名称
String profit = arrVal[2]; //
list.add(order_no);
list.add(member_name);
list.add(profit);
}
log.debug("---------list.size()--"+list.size());
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {if(bfR != null)bfR.close();} catch (IOException e) {e.printStackTrace();}
}
%>
<%
/*---------------- 以下为分页信息 ---------------------*/
/* 分页信息定义 */
//
int total_page = 0; //总页数
int nextPage = 0;
int previouPage = 0;
int this_page = Integer.parseInt(request.getParameter("page")==null? "0":request.getParameter("page")); //当前页码
int PAGE_SIZE = 20; //每页显示条数
String myBeginLink = ""; //首页
String myPreviouLink = ""; //上一页
int TotalRecorders = number ; //总记录数
String myNextLink = ""; //下一页
String myEndLink = ""; //末页
if(number%10 == 0)
{
total_page = number/PAGE_SIZE;
}
else
{
total_page = number/PAGE_SIZE + 1;
}
log.debug("---total_page---:"+total_page);
log.debug("---this_page---:"+this_page);
log.debug("---TotalRecorders---:"+TotalRecorders);
if (TotalRecorders == 0)
{
this_page = 0;
nextPage = 0;
previouPage = 0;
myEndLink = "#";
myNextLink = "#";
myBeginLink = "#";
myPreviouLink = "#";
}
else
{
if (this_page < 1)
this_page = 1;
if (this_page > total_page)
this_page = total_page;
//下一页
nextPage = this_page + 1;
if (nextPage > total_page)
nextPage = total_page;
//上一页
previouPage = this_page - 1;
if (previouPage < 1)
previouPage = 1;
//首页和上一页链接
if(this_page == 1)
{
myBeginLink = "#";
myPreviouLink = "#";
}
else
{
myBeginLink = "orderNo.jsp?FileName="+ FileName +"&page=1"; //首页
myPreviouLink = "orderNo.jsp?FileName="+ FileName +"&page="+previouPage;
}
//下一页和尾页链接
if(this_page == total_page)
{
myNextLink = "#";
myEndLink = "#";
}
else
{
myNextLink = "orderNo.jsp?FileName="+ FileName +"&page="+nextPage;
myEndLink = "orderNo.jsp?FileName="+ FileName +"&page="+total_page;
}
}
%>
以下是页面显示控制:
<table align="center" style="border-collapse:collapse;" bgcolor="#FFFFFF" bordercolor="#AAAAAA" cellspacing="0" cellpadding="0" border="1" width="760">
<tr>
<td><div align="center"><strong class="A6">排名</strong></div></td>
<td><div align="center"><strong class="A6">会员名称</strong></div></td>
<td><div align="center"><strong class="A6"><%=TD_name%></strong></div></td>
</tr>
<%
if(list != null && list.size()>0)
{
//this_page ++;
if(this_page != total_page)
{
for(int k=(this_page-1)*PAGE_SIZE*3; k<(this_page*PAGE_SIZE*3); k+=3 )
{
%>
<tr>
<td><div align="center"><span class="A6"><%=list.get(k)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+1)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+2)%></span></div></td>
</tr>
<% }
}
else if(this_page == total_page)
{
for(int k=(total_page-1)*PAGE_SIZE*3; k<list.size(); k+=3)
{
%>
<tr>
<td><div align="center"><span class="A6"><%=list.get(k)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+1)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+2)%></span></div></td>
</tr>
<%
}
}
}
%>
<tr>
<td colspan="3">
<div align="center"><span class="A6">
<a href="<%=myBeginLink%>">首页 </a> |
<a href="<%=myPreviouLink%>">上一页 </a>
[<%=this_page%>/<%=total_page%>共<%=TotalRecorders %>条记录]
<a href="<%=myNextLink%>">下一页 </a> |
<a href="<%=myEndLink%>">末页 </a>
</span></div>
</td>
</tr>
</table>
BufferedReader bfR = null;
ArrayList list = new ArrayList();
try
{
String str = null;
bfR = new BufferedReader(new InputStreamReader(new FileInputStream(SortFile+FileName), "GB2312"));
while ((str = bfR.readLine()) != null)
{
number++;
String tempTrack = str.trim();
String[] arrVal = new String[3];
arrVal = tempTrack.split("/|");
if (arrVal.length < 3)
continue;
String order_no = arrVal[0]; //排名
String member_name = arrVal[1]; //名称
String profit = arrVal[2]; //
list.add(order_no);
list.add(member_name);
list.add(profit);
}
log.debug("---------list.size()--"+list.size());
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {if(bfR != null)bfR.close();} catch (IOException e) {e.printStackTrace();}
}
%>
<%
/*---------------- 以下为分页信息 ---------------------*/
/* 分页信息定义 */
//
int total_page = 0; //总页数
int nextPage = 0;
int previouPage = 0;
int this_page = Integer.parseInt(request.getParameter("page")==null? "0":request.getParameter("page")); //当前页码
int PAGE_SIZE = 20; //每页显示条数
String myBeginLink = ""; //首页
String myPreviouLink = ""; //上一页
int TotalRecorders = number ; //总记录数
String myNextLink = ""; //下一页
String myEndLink = ""; //末页
if(number%10 == 0)
{
total_page = number/PAGE_SIZE;
}
else
{
total_page = number/PAGE_SIZE + 1;
}
log.debug("---total_page---:"+total_page);
log.debug("---this_page---:"+this_page);
log.debug("---TotalRecorders---:"+TotalRecorders);
if (TotalRecorders == 0)
{
this_page = 0;
nextPage = 0;
previouPage = 0;
myEndLink = "#";
myNextLink = "#";
myBeginLink = "#";
myPreviouLink = "#";
}
else
{
if (this_page < 1)
this_page = 1;
if (this_page > total_page)
this_page = total_page;
//下一页
nextPage = this_page + 1;
if (nextPage > total_page)
nextPage = total_page;
//上一页
previouPage = this_page - 1;
if (previouPage < 1)
previouPage = 1;
//首页和上一页链接
if(this_page == 1)
{
myBeginLink = "#";
myPreviouLink = "#";
}
else
{
myBeginLink = "orderNo.jsp?FileName="+ FileName +"&page=1"; //首页
myPreviouLink = "orderNo.jsp?FileName="+ FileName +"&page="+previouPage;
}
//下一页和尾页链接
if(this_page == total_page)
{
myNextLink = "#";
myEndLink = "#";
}
else
{
myNextLink = "orderNo.jsp?FileName="+ FileName +"&page="+nextPage;
myEndLink = "orderNo.jsp?FileName="+ FileName +"&page="+total_page;
}
}
%>
以下是页面显示控制:
<table align="center" style="border-collapse:collapse;" bgcolor="#FFFFFF" bordercolor="#AAAAAA" cellspacing="0" cellpadding="0" border="1" width="760">
<tr>
<td><div align="center"><strong class="A6">排名</strong></div></td>
<td><div align="center"><strong class="A6">会员名称</strong></div></td>
<td><div align="center"><strong class="A6"><%=TD_name%></strong></div></td>
</tr>
<%
if(list != null && list.size()>0)
{
//this_page ++;
if(this_page != total_page)
{
for(int k=(this_page-1)*PAGE_SIZE*3; k<(this_page*PAGE_SIZE*3); k+=3 )
{
%>
<tr>
<td><div align="center"><span class="A6"><%=list.get(k)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+1)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+2)%></span></div></td>
</tr>
<% }
}
else if(this_page == total_page)
{
for(int k=(total_page-1)*PAGE_SIZE*3; k<list.size(); k+=3)
{
%>
<tr>
<td><div align="center"><span class="A6"><%=list.get(k)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+1)%></span></div></td>
<td><div align="center"><span class="A6"><%=list.get(k+2)%></span></div></td>
</tr>
<%
}
}
}
%>
<tr>
<td colspan="3">
<div align="center"><span class="A6">
<a href="<%=myBeginLink%>">首页 </a> |
<a href="<%=myPreviouLink%>">上一页 </a>
[<%=this_page%>/<%=total_page%>共<%=TotalRecorders %>条记录]
<a href="<%=myNextLink%>">下一页 </a> |
<a href="<%=myEndLink%>">末页 </a>
</span></div>
</td>
</tr>
</table>
展开全部
我封装好了方法,你只要动态的传递startLine和endLine两个参数,就能达到分页的目的了,地址如下!~
http://hi.baidu.com/zhaotao%5Fking/blog/item/d4b79f007dee08137aec2c2b.html
有问题可留言,good luck!~
http://hi.baidu.com/zhaotao%5Fking/blog/item/d4b79f007dee08137aec2c2b.html
有问题可留言,good luck!~
追问
嗯嗯,实现是实现了,但不是分页啊,显示的是选定的那些行。想要的效果是显示在jsp中,进入的时候显示的是第一页,最下面有翻页和跳页的效果(跟很多论坛里面那种)。
追答
你把封装好的方法,写到servlet中就行了,jsp页面上往servlet中传当前页和每页显示多少条,计算出startLine和endLine两个参数。
举个例子
jsp上传pageNo = 1; pa=20; //每页显示20条
那么startLine = pageNo * pa - pa; //这个算出从第几行开始取值
endLine = startLine + pa; //这个算出取到多少行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
页面上传入一个第N页的N的值。读取数据的时候,循环readLine(),把从第N*100行到第(N+1)*100的数据写回页面上。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询