单篇文章的分页

来源:互联网 发布:h5app源码下载 编辑:程序博客网 时间:2024/06/15 01:24

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

我做了个简单的例子

这是主运行程序

 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;
 }
 }