j2me记录仓库,手机电子书实现

东方尚智,3G数字内容学院j2me教程---记录仓库,电子书的实现

视频播放地址:http://www.3gdci.com/article/news/20090914152557.htm

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MainApp extends MIDlet{
public Alert logo;//欢迎界面
public List menu;//书列表页面
public Form content;//书的内容页面
public Command cmd_read=new Command("开使阅读",Command.SCREEN,1);
public Command cmd_goto=new Command("进入书签",Command.SCREEN,1);
public Command cmd_setA=new Command("设定书签",Command.SCREEN,1);
public Command cmd_NextP=new Command("下一页",Command.SCREEN,1);
public Command cmd_PreP=new Command("上一页",Command.SCREEN,1);
public Command cmd_Help=new Command("帮助",Command.SCREEN,1);
public Command cmd_Exit=new Command("退出",Command.SCREEN,1);
public Command cmd_Back=new Command("返回",Command.BACK,1);
public BooKRMS bookrms=new BooKRMS();
public int nowp=1;
public String bkname;
public static MainApp midlet;
public CommandListener action=
new CommandListener()
{ public void commandAction(Command c,Displayable dis)
{
if(c==cmd_Exit){notifyDestroyed();}
if(c==cmd_Help){}
if(c==cmd_NextP)
{content.deleteAll();
if(bkname.indexOf("喜剧之王")>=0){
bkname="xjzw";
}
nowp++;
//获取当前页
String nowpcon=bookrms.getPage(bkname, nowp);
content.append(nowpcon);
}
if(c==cmd_PreP)
{content.deleteAll();
if(bkname.indexOf("喜剧之王")>=0){
bkname="xjzw";
}
nowp--;
//获取当前页
String nowpcon=bookrms.getPage(bkname, nowp);
content.append(nowpcon);
}
if(c==cmd_read)//开始阅读
{ content.deleteAll();
int n=menu.getSelectedIndex();
bkname=menu.getString(n);
content.setTitle(bkname);
if(bkname.indexOf("喜剧之王")>=0){
bkname="xjzw";
}
nowp=1;
//获取当前页
String nowpcon=bookrms.getPage(bkname, nowp);
content.append(nowpcon);
Display.getDisplay(midlet).setCurrent(content);
}
if(c==cmd_setA)
{
bookrms.saveA(bkname, nowp);
Alert sms=new Alert("成功");
sms.setTimeout(1000);
sms.setString("书签设定成功");
Display.getDisplay(midlet).setCurrent(sms,content);
}
if(c==cmd_goto)//进入书签
{
int n=menu.getSelectedIndex();
String bkname=menu.getString(n);
content.setTitle(bkname);
if(bkname.indexOf("喜剧之王")>=0){
bkname="xjzw";
}
//获取当前页 获取书签
String nowp=bookrms.getPage(bkname,bookrms.getA(bkname));
content.append(nowp);
Display.getDisplay(midlet).setCurrent(content);

}
if(c==cmd_Back)
{
Display.getDisplay(midlet).setCurrent(menu);
}
}
};

public MainApp()
{ midlet=this;
logo=new Alert("2009最受欢迎的手机软件");
try{
Image im=Image.createImage("/logo.png");
logo.setImage(im);
}catch(Exception e){}
logo.setTimeout(3000);
menu=new List("经典电影剧本",List.IMPLICIT);
menu.append("《月光宝盒》", null);
menu.append("《仙履奇缘》", null);
menu.append("《唐伯虎点秋香》", null);
menu.append("《算死草》", null);
menu.append("《喜剧之王》", null);
menu.append("《食神》", null);
menu.append("《国产零零柒》", null);
menu.append("《大内密探零零发》", null);
menu.append("《武状元苏乞儿》", null);
menu.append("《龙过鸡年》", null);
menu.append("《破坏之王》", null);
menu.append("《少林足球》 ", null);
menu.append("《鹿鼎记》 ", null);
menu.append("《神龙教》 ", null);
menu.addCommand(cmd_read);
menu.addCommand(cmd_goto);
menu.addCommand(cmd_Help);
menu.addCommand(cmd_Exit);
menu.setCommandListener(action);
content=new Form("");
content.addCommand(cmd_Back);
content.addCommand(cmd_PreP);
content.addCommand(cmd_NextP);
content.addCommand(cmd_goto);
content.addCommand(cmd_setA);
content.setCommandListener(action);

}
public void startApp()
{
Display.getDisplay(this).setCurrent(logo,menu);
bookrms.setUpBook();//安装所有书到RMS
}
public void destroyApp(boolean f)
{}
public void pauseApp()
{}

}
//

import java.io.*;

import javax.microedition.rms.RecordStore;
//完成图书操作功能
public class BooKRMS {
private String getPageFromFile(String bname,int n)
{
InputStream is=this.getClass().getResourceAsStream("/"+bname+"/"+bname+"0"+n+".txt");
StringBuffer sb=new StringBuffer();
String con="";
int tmp=0;
try{
while(tmp!=-1)//-1表示文件结束
{
tmp=is.read();//读一个字符
sb.append((char)tmp);
}
con=new String(sb);
con=new String(con.getBytes("iso8859-1"),"utf-8");
//System.out.println("读取了"+con);
}catch(Exception ee){}
return con;
}
private void savePageToRMS(String bname,String n)
{try{
RecordStore rs=RecordStore.openRecordStore(bname, true);
byte dat[]=n.getBytes();
rs.addRecord(dat, 0, dat.length);
rs.closeRecordStore();
System.out.println("save ok");
}catch(Exception e){}
}
public void setUpBook()//安装图书到RMS
{ try{
RecordStore rs=RecordStore.openRecordStore("bkinfo", false);
return;
}catch(Exception e){
try{
RecordStore rs=RecordStore.openRecordStore("bkinfo", true);
rs.closeRecordStore();
}catch(Exception e2){}
}
//得到每本图书的所有页
//存到RMS
for(int i=1;i<=3;i++)//先完成第一本书
{
String nowp=this.getPageFromFile("xjzw",i);
this.savePageToRMS("xjzw", nowp);
}
/...
}
// 获取某本图书的第n页
public String getPage(String bname,int n)
{String con="";
try{
RecordStore rs=RecordStore.openRecordStore(bname, true);
byte dat[]=rs.getRecord(n);
con=new String(dat);
rs.closeRecordStore();
System.out.println("open ");
}catch(Exception e){}
return con;
}
//保存书签
public void saveA(String bkname,int p)
{try{
RecordStore rs=RecordStore.openRecordStore(bkname+"a", true);
byte dat[]=new byte[4];
dat[0]=(byte)(p>>24);
dat[1]=(byte)(p>>16);
dat[2]=(byte)(p>>8);
dat[3]=(byte)p;
if(rs.getNumRecords()>0)
rs.setRecord(1,dat, 0, dat.length);
else
rs.addRecord(dat, 0, dat.length);
rs.closeRecordStore();
System.out.println("open ");
}catch(Exception e){}
}
//读取书签
public int getA(String bkname)
{ int p=1;
try{
RecordStore rs=RecordStore.openRecordStore(bkname+"a", true);
byte dat[]=rs.getRecord(1);
p=dat[0]<<24+dat[1]<<16+dat[2]<<8+dat[3];
rs.closeRecordStore();
System.out.println("open ");
}catch(Exception e){
return 1;
}
return p;
}
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sdhjob/archive/2009/09/22/4579471.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值