分享
 
 
 

联网并进行RMS永久存储的演示代码

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

Stock.Java

public class Stock {

private static String name, time, price;

public static void parse(String data) {

int index = data.indexOf('"');

name = data.substring(++index, (index = data.indexOf('"', index)));

index +=3;

time = data.substring(index, (index = data.indexOf('-', index))-1);

index +=5;

price = data.substring(index, (index = data.indexOf('<', index)));

}

public static String getName(String record) {

parse(record);

return(name);

}

public static String getPrice(String record) {

parse(record);

return(price);

}

}

StockDB.java

import javax.microedition.rms.*;

import java.util.Enumeration;

import java.util.Vector;

import java.io.*;

public class StockDB {

RecordStore recordStore = null;

public StockDB() {}

public StockDB(String fileName) {

try {

recordStore = RecordStore.openRecordStore(fileName, true);

} catch(RecordStoreException rse) {

rse.printStackTrace();

}

}

public void close() throws RecordStoreNotOpenException,

RecordStoreException {

if (recordStore.getNumRecords() == 0) {

String fileName = recordStore.getName();

recordStore.closeRecordStore();

recordStore.deleteRecordStore(fileName);

} else {

recordStore.closeRecordStore();

}

}

public synchronized void addNewStock(String record) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream outputStream = new DataOutputStream(baos);

try {

outputStream.writeUTF(record);

}

catch (IOException ioe) {

System.out.println(ioe);

ioe.printStackTrace();

}

byte[] b = baos.toByteArray();

try {

recordStore.addRecord(b, 0, b.length);

}

catch (RecordStoreException rse) {

System.out.println(rse);

rse.printStackTrace();

}

}

public synchronized RecordEnumeration enumerate() throws RecordStoreNotOpenException {

return recordStore.enumerateRecords(null, null, false);

}

}

QuotesMIDlet.java

import javax.microedition.rms.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import java.io.*;

import java.util.Vector;

public class QuotesMIDlet extends MIDlet implements CommandListener {

Display display = null;

List menu = null; // main menu

List choose = null;

TextBox input = null;

Ticker ticker = new Ticker("Database Application");

String quoteServer = "http://quote.yahoo.com/d/quotes.csv?s=";

String quoteFormat = "&f=slc1wop";

static final Command backCommand = new Command("Back", Command.BACK, 0);

static final Command mainMenUCommand = new Command("Main", Command.SCREEN, 1);

static final Command saveCommand = new Command("Save", Command.OK, 2);

static final Command exitCommand = new Command("Exit", Command.STOP, 3);

String currentMenu = null;

// Stock data

String name, date, price;

StockDB db = null;

public QuotesMIDlet() {

}

public void startApp() throws MIDletStateChangeException {

display = Display.getDisplay(this);

// open a db stock file

try {

db = new StockDB("stocks");

} catch(Exception e) {}

menu = new List("Stocks Database", Choice.IMPLICIT);

menu.append("List Stocks", null);

menu.append("Add A New Stock", null);

menu.addCommand(exitCommand);

menu.setCommandListener(this);

menu.setTicker(ticker);

mainMenu();

}

public void pauseApp() {

display = null;

choose = null;

menu = null;

ticker = null;

try {

db.close();

db = null;

} catch(Exception e) {}

}

public void destroyApp(boolean unconditional) {

try {

db.close();

} catch(Exception e) {}

notifyDestroyed();

}

void mainMenu() {

display.setCurrent(menu);

currentMenu = "Main";

}

public String tickerString() {

StringBuffer ticks = null;

try {

RecordEnumeration enum = db.enumerate();

ticks = new StringBuffer();

while(enum.hasNextElement()) {

String stock1 = new String(enum.nextRecord());

ticks.append(Stock.getName(stock1));

ticks.append(" @ ");

ticks.append(Stock.getPrice(stock1));

ticks.append(" ");

}

} catch(Exception ex) {}

return (ticks.toString());

}

public void addStock() {

input = new TextBox("Enter a Stock Name:", "", 5, TextField.ANY);

input.setTicker(ticker);

input.addCommand(saveCommand);

input.addCommand(backCommand);

input.setCommandListener(this);

input.setString("");

display.setCurrent(input);

currentMenu = "Add";

}

public String getQuote(String input) throws IOException, NumberFormatException {

String url = quoteServer + input + quoteFormat;

StreamConnection c = (StreamConnection)Connector.open(url, Connector.READ_WRITE);

InputStream is = c.openInputStream();

StringBuffer sb = new StringBuffer();

int ch;

while((ch = is.read()) != -1) {

sb.append((char)ch);

}

return(sb.toString());

}

public void listStocks() {

choose = new List("Choose Stocks", Choice.MULTIPLE);

choose.setTicker(new Ticker(tickerString()));

choose.addCommand(backCommand);

choose.setCommandListener(this);

try {

RecordEnumeration re = db.enumerate();

while(re.hasNextElement()) {

String theStock = new String(re.nextRecord());

choose.append(Stock.getName(theStock)+" @ " + Stock.getPrice(theStock), null);

}

} catch(Exception ex) {}

display.setCurrent(choose);

currentMenu = "List";

}

public void commandAction(Command c, Displayable d) {

String label = c.getLabel();

if (label.equals("Exit")) {

destroyApp(true);

} else if (label.equals("Save")) {

if(currentMenu.equals("Add")) {

// add it to database

try {

String userInput = input.getString();

String pr = getQuote(userInput);

db.addNewStock(pr);

ticker.setString(tickerString());

} catch(IOException e) {

} catch(NumberFormatException se) {

}

mainMenu();

}

} else if (label.equals("Back")) {

if(currentMenu.equals("List")) {

// go back to menu

mainMenu();

} else if(currentMenu.equals("Add")) {

// go back to menu

mainMenu();

}

} else {

List down = (List)display.getCurrent();

switch(down.getSelectedIndex()) {

case 0: listStocks();break;

case 1: addStock();break;

}

}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有