单篇文章的分页
来源:互联网 发布: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> ";
}
for(int i = 1; i<=totalPage; i++){
if (i==correntPage){
pageStr += i + " ";
}else{
pageStr += "<a href=/'"+id+"_"
+ i + ".html" + "'>" + i
+ "</a> ";
}
}
if(correntPage<totalPage){
pageStr += "<a href=/'"+id+"_"
+ (correntPage+1) + ".html" + "'>" + ">>"
+ "</a> ";
}
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;
}
}
- 单篇文章的分页
- 单篇文章以长度分页
- 单篇文章以字符分页
- 对于单篇文章静态分页
- java+jstl 单篇文章分页和列表页面的分页 代码
- java+jstl 单篇文章分页和列表页面的分页 代码
- 请问定制的标签-怎么输出单篇文章
- 如何调用单篇文章内的所有图片附件?
- wordpress单篇文章模板代码
- JS+CSS之单篇文章内容分页
- DEDE文章列表获取单篇文章TAGS解决方法
- ECSHOP 首页根据ID调用单篇文章代码
- 长篇文章的分页
- 大文章的分页
- 大文章的分页
- js的大文章分页
- 单篇 Hibernate应用中Java对象的状态
- 文章分页
- 20070906
- 3389登录日志清除软件
- 实习程序收藏
- flash 避开微软新ActiveX控件升级的方法
- WebService 动态 IP 配置
- 单篇文章的分页
- 条码打印机编程使用方法 C#
- C语言中指针和数组名小趣
- Linux 守护进程的编程方法
- Log actively being archived by another process的问题解决思路
- 字符串中的单引号和双引号的差别
- 宽以待人严于律已
- Word 无法显示图片
- [转]嵌入式数据库系统Berkeley DB