2008年3月15日
#
j2me如何讀取網(wǎng)上資源文件例如文本文件,圖形文件。
例如,讀取www.kingdart.cn/jaccount/imobile.png 轉(zhuǎn)換為Image
又例如:讀取www.kingdart.cn/jaccount/readme.txt 轉(zhuǎn)換為String
只在模擬器上成功我也會,要求是真機上成功!
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174850
發(fā)表時間: 2008年03月22日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
我的錯!在沒有認真閱讀FileConnection文檔之后就妄下結(jié)論.最近下載了fileconnection_spec_1.00文檔,發(fā)現(xiàn)其中有一個方法
public java.io.OutputStream openOutputStream(long byteOffset)
throws java.io.IOException
該方法在打開輸出流時可指定寫入的位置,寫入的數(shù)據(jù)將覆蓋舊數(shù)據(jù),利用這個方法,還是有可能在手機上實現(xiàn)文件式RMS的.
現(xiàn)在我正在做手機理財JAccount的文件備份和恢復(fù),還分不出身來嘗試,有興趣的朋友可以自已試一下如果OK了,別忘了告訴我一聲哦!
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174850
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174754
發(fā)表時間: 2008年03月22日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
由于要為手機理財JAccount增加數(shù)據(jù)導(dǎo)出到文本文件功能,我為其增加了exportToFile(String fileName)方法,使用Moto模擬器(A630)時發(fā)現(xiàn)裝入JAR階段已出錯,錯誤的信息是:
ALERT: Unable to load class javax/microedition/io/file/FileConnection,RAZR_V3則正常.要知道,我從未打算為不同的手機制作不同的JAR,我計劃是在代碼中檢查該手機是否支持FileConnection,若支持的話,菜單項才增加備份和恢復(fù)命令項.
如果所有不支持FileConnection的手機都不能裝入的話,那不是只能為支持的開發(fā)一個版本,不支持的又開發(fā)另一個版本?
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174754
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174645
發(fā)表時間: 2008年03月21日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
/**
* --------------------------------------------------
* 字段輸入流
* --------------------------------------------------
* 從DataInputStream繼承
* 主要增加了從文本格式輸入流中讀入數(shù)據(jù)字段的能力
* --------------------------------------------------
*
* @author iwinyeah 李永超
* @version 1.0.0
* */
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FieldInputStream extends DataInputStream {
public final static int BIN_MODE = 0;
public final static int TXT_MODE = 1;
int mode;
public FieldInputStream(InputStream in, int mode) {
super(in);
this.mode = mode;
}
public boolean getBoolean() throws IOException {
if (mode == 0) {
return readBoolean();
} else {
if ("1".equals(next())) {
return true;
}
return false;
}
}
public byte getByte() throws IOException {
if (mode == 0) {
return readByte();
} else {
return (byte) Integer.parseInt(next());
}
}
public short getShort() throws IOException {
if (mode == 0) {
return readShort();
} else {
return (short) Integer.parseInt(next());
}
}
public int getInt() throws IOException {
if (mode == 0) {
return readInt();
} else {
return Integer.parseInt(next());
}
}
public long getLong() throws IOException {
if (mode == 0) {
return readLong();
} else {
return Long.parseLong(next());
}
}
public String getString() throws IOException {
if (mode == 0) {
if (read() == 0) {
return null;
} else {
return readUTF();
}
} else {
return next();
}
}
// 取下一標(biāo)識符
private byte[] buffer = new byte[255];
private int length = 0;
private boolean eos = false;
private final static int INITIAL = 0;
private final static int ESCAPE = 1;
private final static int COMMENT_START = 2;
private final static int LINE_COMMENT = 3;
private final static String WHITESPACE = "\n\r\t";
public String next() throws IOException {
length = 0;
int c = in.read();
int status = INITIAL;
READWHILE: while (c != -1) { // 一直讀到文件尾
switch (status) {
case INITIAL:
if (c == '\n' || c == '\t') { // 如果是分隔符
break READWHILE;
} else if (c == '\\') {
status = ESCAPE; // 設(shè)轉(zhuǎn)義字符標(biāo)志
} else if (c == '/') {
status = COMMENT_START; // 設(shè)注釋標(biāo)志
} else {
if (WHITESPACE.indexOf(c) < 0) {
append(c);
}
}
break;
case ESCAPE: // 處理轉(zhuǎn)義字符
switch (c) {
case 'n':
append('\n');
break;
case 'r':
append('\r');
break;
case 't':
append('\t');
break;
case 'b':
append('\b');
break;
case 'f':
append('\f');
break;
default:
append(c);
break;
}
status = INITIAL; // 設(shè)正常情況標(biāo)志
break;
case COMMENT_START: // 處理注釋
if (c == '/') {
status = LINE_COMMENT; // 是行式注釋
} else {
status = INITIAL;
// 如果都不是則把注釋起始符和剛讀入的字符都加入到標(biāo)識符中
append('/');
append(c);
}
break;
case LINE_COMMENT:
if (c == '\n') {
status = INITIAL; // 如果當(dāng)前為行注釋狀態(tài)則要一直讀到行尾才恢復(fù)正常情況標(biāo)志
break READWHILE;
}
break;
}
c = in.read(); // 讀入下一字符
}
if (c == -1) {
eos = true;
}
// 如果讀到文件尾時,標(biāo)識符長度大于零,則返回標(biāo)識符,否則返回NULL值
if (length <= 0) {
return null;
} else {
return new String(buffer, 0, length, "UTF-8");
}
}
// 將讀入的字符加入緩沖區(qū)
private void append(int c) {
// 緩沖區(qū)不足時自動擴展
if (length >= buffer.length) {
byte[] xBuffer = new byte[buffer.length + 16];
System.arraycopy(buffer, 0, xBuffer, 0, buffer.length);
buffer = null;
buffer = xBuffer;
}
buffer[length++] = (byte) c;
}
public boolean eos() {
return eos;
}
}
請參看我的另一篇文章:字段輸出流FieldOutputStream
http://iwinyeah.javaeye.com/admin/blogs/174644
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174645
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174644
發(fā)表時間: 2008年03月21日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
我的FieldOutputStream繼承了DataOutputStream,這樣就可以只更改很少量的代碼就實現(xiàn)了既支持原生格式又支持文本方式輸出了,稍候一段時間手機理財將可以實現(xiàn)備份和恢復(fù)(文本格式)功能了.
package util;
/**
* --------------------------------------------------
* 字段輸出流
* --------------------------------------------------
* 從DataOutputStream繼承
* 主要增加了向輸出流寫入文本格式的數(shù)據(jù)字段的能力
* 文本格式流將由TAB分隔字段,回車換行符分隔記錄
* --------------------------------------------------
*
* @author iwinyeah 李永超
* @version 1.0.0
* */
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FieldOutputStream extends DataOutputStream {
public final static int BIN_MODE = 0;
public final static int TXT_MODE = 1;
private final static byte[] fieldSplit = {'\t'};
private final static byte[] recordSplit = {'\r','\n'};
private int mode;
private boolean nextEnd = false;
public FieldOutputStream(OutputStream out, int mode) {
super(out);
this.mode = mode;
}
// 接著寫入的是否最后一個字段
// 寫第一個字段前以參數(shù)false調(diào)用它
// 寫最后一個字段前以參數(shù)false調(diào)用它
public void setNextEnd(boolean end){
nextEnd = end;
}
public void putBoolean(boolean value) throws IOException {
if (mode == 0) {
writeBoolean(value);
} else {
out.write(value ? '1' : '0');
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
public void putByte(byte value) throws IOException {
if (mode == 0) {
writeByte(value);
} else {
out.write(String.valueOf(value).getBytes("UTF-8"));
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
public void putShort(short value) throws IOException {
if (mode == 0) {
writeShort(value);
} else {
out.write(String.valueOf(value).getBytes("UTF-8"));
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
public void putInt(int value) throws IOException {
if (mode == 0) {
writeInt(value);
} else {
out.write(String.valueOf(value).getBytes("UTF-8"));
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
public void putLong(long value) throws IOException {
if (mode == 0) {
writeLong(value);
} else {
out.write(String.valueOf(value).getBytes("UTF-8"));
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
public void putString(String value) throws IOException {
if (mode == 0) {
if (value == null) {
writeByte(0);
} else {
writeByte(1);
writeUTF(value);
}
} else {
if(value != null){
byte[] b = value.getBytes("UTF-8");
for(int i = 0; i < b.length; i++){
if(b[i] == '\n'){
out.write('\\');
out.write('n');
}
else if(b[i] == '\r'){
out.write('\\');
out.write('r');
}
else if(b[i] == '\t'){
out.write('\\');
out.write('t');}
else if(b[i] == '\b'){
out.write('\\');
out.write('b');}
else if(b[i] == '\f'){
out.write('\\');
out.write('f');
}else{
out.write(b[i]);
}
}
}
out.write(nextEnd ? recordSplit : fieldSplit);
}
}
}
讀回請參看另一篇:字段輸入流FieldInputStream.
http://iwinyeah.javaeye.com/admin/blogs/174645
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174644
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/173704
發(fā)表時間: 2008年03月19日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
我的一個日期處理類,解決了時區(qū)問題,給有需要的人。
package util;
/**
* --------------------------------------------------
* 日期轉(zhuǎn)換對象
* --------------------------------------------------
* 主要提供日期與1970-01-01后的天數(shù)的轉(zhuǎn)換和到字符串的轉(zhuǎn)換
* --------------------------------------------------
*
* @author iwinyeah 李永超
* @version 1.0.0
* */
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateUtil {
private static Calendar _calendar = Calendar.getInstance(); // 用于日期計算
private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒數(shù)
private static int rawOffset = TimeZone.getDefault().getRawOffset();
/**
* 將日期轉(zhuǎn)換為1970-01-01后的天數(shù)
*
* @param Date
* theDate 要計算天數(shù)的日期
* @return int 所傳入日期與1970-01-01相差的天數(shù)
*/
public static int dateToDay(Date theDate) {
return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
}
/**
* 將1970-01-01后的天數(shù)轉(zhuǎn)換為日期
*
* @param int
* 要取的日期與1970-01-01相差的天數(shù)
* @return Date theDate 與1970-01-01相差相應(yīng)天數(shù)的日期
*/
public static Date dayToDate(int day) {
return new Date(day * MSEC_EVERYDAY);
}
/**
* 取今天與1970-01-01相差的天數(shù)
*
* @return int 取今天與1970-01-01相差的天數(shù)
*/
public static int toDay() {
return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
}
/**
* 將日期轉(zhuǎn)換為年月日字符串
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return String 對應(yīng)日期年月日形式的字符串
*/
public static String getYMD(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR) % 100 + "/"
+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1) + "/"
+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
+ _calendar.get(Calendar.DATE);
}
/**
* 將日期轉(zhuǎn)換為年月字符串
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return String 對應(yīng)日期年月形式的字符串
*/
public static String getYM(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR) + "/"
+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1);
}
/**
* 將日期轉(zhuǎn)換為月日字符串
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return String 對應(yīng)日期月日形式的字符串
*/
public static String getMD(int theDay) {
_calendar.setTime(dayToDate(theDay));
return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
+ (_calendar.get(Calendar.MONTH) + 1) + "/"
+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
+ _calendar.get(Calendar.DATE);
}
/**
* 將日期轉(zhuǎn)換為當(dāng)月一號
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return int 對應(yīng)日期所在月份第一天與1970-01-01相差的天數(shù)
*/
public static int getMonthFirstDay(int theDay) {
_calendar.setTime(dayToDate(theDay));
_calendar.set(Calendar.DAY_OF_MONTH, 1);
return (int) (dateToDay(_calendar.getTime()));
}
/**
* 取日期所在年份
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return int 對應(yīng)日期所在年份
*/
public static int getYear(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR);
}
/**
* 取日期所在月份
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return int 對應(yīng)日期所在月份
*/
public static int getMonth(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.MONTH);
}
/**
* 取日期所在周次
*
* @param int
* theDay 與1970-01-01相差的天數(shù)
* @return int 對應(yīng)日期所在周次
*/
public static int getWeek(int theDay) {
// 1971-01-03是星期日,從該日開始計算周次
_calendar.setTime(dayToDate(theDay));
return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);
}
}
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/173704
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172974
發(fā)表時間: 2008年03月17日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
Action: 規(guī)定了與用戶交互的View可以觸發(fā)的動作,在某個View新建之后顯示之前,應(yīng)先為其指定具體的Action,當(dāng)用戶按下了相應(yīng)的Command按鈕之后,View將該Command對應(yīng)的Action發(fā)送到該View的Controller進行處理。
//
public class Action{
String name; // 名稱
Command command; // 命令
int code; // 代碼 (將由該View的傳遞到其Controller使用)
Item item; // 數(shù)據(jù)項
boolean defaultAction; // 是否是默認的Action
//...省略
}
請看View的基類的代碼節(jié)選
public abstract class AbstractView{
//...省略
// 為該View增加Action
public void addAction( final Action action, final boolean active )
{
if( !actions.containsKey( action.getName() ) )
{
// 將Action存入Actions表中
actions.put( action.getName(), action );
if( active )
{
activateAction( action );
}
}
}
// 使Action生效可用
private void activateAction( final Action action )
{
final Command command = action.getCommand();
activeActions.put( command, action );
final Item item = action.getItem();
if( item == null )
{
addCommand( command ); // 該Action是屏幕相關(guān)的命令
}
else
{
item.addCommand( command ); // 該Action是數(shù)據(jù)項相關(guān)的命令
if( action.isDefaultAction() )
{
item.setDefaultCommand( command );
}
}
}
//...省略
// 用戶按下相應(yīng)的命令鍵后,觸發(fā)執(zhí)行與其關(guān)聯(lián)的Action
public void commandAction(
final Command command,
final Displayable displayable
)
{
if( !handleAction( command ) )
{
if( displayable instanceof Choice )
{
AbstractController.commandAction(
this,
command,
(Choice) displayable
);
}
else
{
AbstractController.commandAction( this, command );
}
}
}
// 用戶在某個指定了命令的Item按下了命令按鈕時觸發(fā)執(zhí)行與其關(guān)聯(lián)的Action
public void commandAction( final Command command, final Item item )
{
if( !handleAction( command ) )
{
AbstractController.commandAction( this, command );
}
}
// 根據(jù)所觸發(fā)的命令查找關(guān)聯(lián)的Action,并新它發(fā)送到Controller進行處理
public boolean handleAction( final Command command )
{
if( activeActions.containsKey( command ) )
{
final Action action = (Action) activeActions.get( command );
// 以Action代碼為參數(shù)生成ControllerEvent并傳遞到controller處理
final ControllerEvent event = new ControllerEvent(
action.getCode(),
this
);
controller.handle( event );
return true;
}
else
{
return false;
}
}
//...省略
}
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/172974
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172237
發(fā)表時間: 2008年03月15日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
經(jīng)過多次的嘗試,終于解決了手機理財JAccount在Nokia 6070反復(fù)統(tǒng)計收支表和余額表時出錯的問題.
原來我有兩個報表上分別使用了incomeVector 和balanceVector來保存所生成的統(tǒng)計資料,每次統(tǒng)計前檢查Vector是否為null,否則先置空,再重新new一個.
我嘗試過new之后加了runtime.gc(),未能解決問題;
我又嘗試過不置空Vector,而使用vector.removeallelements(),也不行;
我又嘗試過兩個報表共用一個Vector也不行;
最后,我使用兩個報表共用數(shù)組來保存結(jié)果,才解決了問題,有點開心.
類似Nokia6070這種機器的JVM的內(nèi)存管理的確存在很大的問題,明明有內(nèi)存也用不得,真郁悶!
不過還是有點開心,畢竟解決了一個問題!
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/172237
網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172200
發(fā)表時間: 2008年03月15日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書面許可,嚴禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
我以前在其他地方發(fā)過的貼子,這幾種方式都沒問題,哪種較好或者說在什么情況下用哪種方法較好呢?
// 方法一
public class firstManager implements Runnable {
public void runTask() {
(new Thread(this)).start();
}
public void run() {
System.out.println("\nfirst thread method!");
// Do some thing ...
}
}
// 方法二
public class secondManager {
private BackTask backTask;
private Timer timer;
public secondManager() {
backTask = new BackTask();
timer = new Timer();
}
public void runTask() {
timer.schedule(backTask, 0);
}
private class BackTask extends TimerTask {
public void run() {
System.out.println("\nsecond thread method!");
// Do some thing ...
}
}
}
// 方法三
public class thirdManager {
private BackTask backTask;
private int cmd = 0;
public thirdManager() {
backTask = new BackTask();
backTask.start();
}
public void runTask() {
synchronized (backTask) {
cmd = 1;
backTask.notify();
}
}
private class BackTask extends Thread {
public void run() {
while (true) {
try {
if (cmd == 0) {
synchronized (this) {
wait();
}
continue;
}
System.out.println("\nthird thread method!");
// Do some thing ...
} catch (Exception e) {
}
cmd = 0;
}
}
}
}
// 用例
public void main(){
firstManager man1 = new firstManager();
secondManager man2 = new secondManager();
thirdManager man3 = new thirdManager();
man1.runTask();
man2.runTask();
man3.runTask();
}
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/172200