昨天在工作中遇到了一个问题,客户要求从文件中(文件类型 .txt ) 读取出排名信息,然后分页来显示,以前做分页大多是从数据库中读取出数据,然后分页,从来没有试过读文件然后分页的。于是在网上搜索了一下~ 发现有很多帖子也在问这个问题,但是都没有很好的回答,然后我就试着自己做了下~ 没想到竟然可以了。呵呵~~ 对高手来说应该是很简单的问题吧,不过对半瓶子的我来说还是有点成功的兴奋哈,把代码贴出来,供与我类似的初学者讨论:
只写有用的JSP代码,页面样式用 。。。代替了
<%
...
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;
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;