单篇文章的分页

  对于一篇过长的文章如果用一页来显示拖动起来比较麻烦,这就需要给这篇文章来进行分页显示

我做了个简单的例子

这是主运行程序

 package com.caexpo.dao;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.TestCase;

public class SpliteTest extends TestCase {

 public void testGetSp() throws FileNotFoundException {
  
  try {
   String filePath = "c://temp.html";///仅仅是个模板
   String templateContent = "";
   FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模块文件
   int lenght = fileinputstream.available();
   byte bytel[] = new byte[lenght];
   fileinputstream.read(bytel);
   fileinputstream.close();
   //templateContent = new String(bytel);

   FileReader fr = new FileReader("c://he.html"); ///读入数据
   BufferedReader br = new BufferedReader(fr);

   String asd = "";
   String str = "";
   while ((asd = br.readLine()) != null) {

    str += asd;  //读出的数据加到字符串中
   }
   ArticlePage ap=new ArticlePage(); //分页逻辑

Splite sp = Splite.getInstance();//拆分类

   String sstr="he";
   String[] art = sp.spliteArticle(str, 200);
   for (int i = 1; i < art.length; i++) {
    templateContent = new String(bytel);
    String page=ap.writePage(i, art.length-1,sstr);
    System.out.println( art.length);
    templateContent = templateContent.replaceAll("<c:content/>",
      art[i]);
    templateContent = templateContent.replaceAll("<c:page/>", page);
    FileOutputStream fos = new FileOutputStream("c://he_" + i
      + ".html");

    byte bytes[] = templateContent.getBytes();
    fos.write(bytes);
    fos.close();
   }
  } catch (IOException ex) {
   ex.getStackTrace();
  }
 }

}

 

分页逻辑

public class ArticlePage {
 
 private int correntPage;
 
 private int totalpage;
 
 public ArticlePage(){}
 
 public ArticlePage(int totalPage){
  this.totalpage = totalPage;
  this.correntPage = 1;
 }
 public String writePage(int correntPage, int totalPage,String id){
  String pageStr="";
  if (correntPage>1){
   pageStr += "<a href=/'" + id+"_"+
    (correntPage-1) + ".html" + "'>"+"<<" 
   + "</a>&nbsp;&nbsp;";
  }
       
  for(int i = 1; i<=totalPage; i++){
   if (i==correntPage){
    pageStr += i + "&nbsp;&nbsp;";
   }else{
    pageStr += "<a href=/'"+id+"_"
    + i + ".html" + "'>" + i
    + "</a>&nbsp;&nbsp;";
   }
  }

  if(correntPage<totalPage){
   pageStr += "<a href=/'"+id+"_"
    + (correntPage+1) + ".html" + "'>" + ">>"
    + "</a>&nbsp;&nbsp;";
  }
  return pageStr;
 }

 public int getCorrentPage() {
  return correntPage;
 }

 public void setCorrentPage(int correntPage) {
  this.correntPage = correntPage;
 }

 public int getTotalpage() {
  return totalpage;
 }

 public void setTotalpage(int totalpage) {
  this.totalpage = totalpage;
 }
}

拆分方法

public class Splite {
 
 private static Splite instance = null;
 private Splite(){}
 
 public static synchronized Splite getInstance() {
  if (instance  == null){
   instance  = new Splite();
  }
  return instance;
 }
 
 
 /**
  * 将长文章截成几段
  * @param articles
  * @return
  */
 public String[] spliteArticle(String articles,int spliteNum){
  String article[] = null;
   int i = articles.length()/spliteNum;
  if ((articles.length()%spliteNum)>0){
   i++;
  }
  article = new String[i];
  for(int k = 0;k<i;k++){
   if((k*spliteNum+spliteNum)<articles.length()){
    article[k] = articles.substring(k*spliteNum,k*spliteNum+spliteNum);
   }else{
    article[k] = articles.substring(k*spliteNum);
   }
  }
  return article;
 }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值