2008年3月17日
#
j2me如何讀取網上資源文件例如文本文件,圖形文件。
例如,讀取www.kingdart.cn/jaccount/imobile.png 轉換為Image
又例如:讀取www.kingdart.cn/jaccount/readme.txt 轉換為String
只在模擬器上成功我也會,要求是真機上成功!
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174850
發表時間: 2008年03月22日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
我的錯!在沒有認真閱讀FileConnection文檔之后就妄下結論.最近下載了fileconnection_spec_1.00文檔,發現其中有一個方法
public java.io.OutputStream openOutputStream(long byteOffset)
throws java.io.IOException
該方法在打開輸出流時可指定寫入的位置,寫入的數據將覆蓋舊數據,利用這個方法,還是有可能在手機上實現文件式RMS的.
現在我正在做手機理財JAccount的文件備份和恢復,還分不出身來嘗試,有興趣的朋友可以自已試一下如果OK了,別忘了告訴我一聲哦!
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174850
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174754
發表時間: 2008年03月22日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
由于要為手機理財JAccount增加數據導出到文本文件功能,我為其增加了exportToFile(String fileName)方法,使用Moto模擬器(A630)時發現裝入JAR階段已出錯,錯誤的信息是:
ALERT: Unable to load class javax/microedition/io/file/FileConnection,RAZR_V3則正常.要知道,我從未打算為不同的手機制作不同的JAR,我計劃是在代碼中檢查該手機是否支持FileConnection,若支持的話,菜單項才增加備份和恢復命令項.
如果所有不支持FileConnection的手機都不能裝入的話,那不是只能為支持的開發一個版本,不支持的又開發另一個版本?
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/174754
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174645
發表時間: 2008年03月21日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
/**
* --------------------------------------------------
* 字段輸入流
* --------------------------------------------------
* 從DataInputStream繼承
* 主要增加了從文本格式輸入流中讀入數據字段的能力
* --------------------------------------------------
*
* @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();
}
}
// 取下一標識符
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; // 設轉義字符標志
} else if (c == '/') {
status = COMMENT_START; // 設注釋標志
} else {
if (WHITESPACE.indexOf(c) < 0) {
append(c);
}
}
break;
case ESCAPE: // 處理轉義字符
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; // 設正常情況標志
break;
case COMMENT_START: // 處理注釋
if (c == '/') {
status = LINE_COMMENT; // 是行式注釋
} else {
status = INITIAL;
// 如果都不是則把注釋起始符和剛讀入的字符都加入到標識符中
append('/');
append(c);
}
break;
case LINE_COMMENT:
if (c == '\n') {
status = INITIAL; // 如果當前為行注釋狀態則要一直讀到行尾才恢復正常情況標志
break READWHILE;
}
break;
}
c = in.read(); // 讀入下一字符
}
if (c == -1) {
eos = true;
}
// 如果讀到文件尾時,標識符長度大于零,則返回標識符,否則返回NULL值
if (length <= 0) {
return null;
} else {
return new String(buffer, 0, length, "UTF-8");
}
}
// 將讀入的字符加入緩沖區
private void append(int c) {
// 緩沖區不足時自動擴展
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
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/174644
發表時間: 2008年03月21日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
我的FieldOutputStream繼承了DataOutputStream,這樣就可以只更改很少量的代碼就實現了既支持原生格式又支持文本方式輸出了,稍候一段時間手機理財將可以實現備份和恢復(文本格式)功能了.
package util;
/**
* --------------------------------------------------
* 字段輸出流
* --------------------------------------------------
* 從DataOutputStream繼承
* 主要增加了向輸出流寫入文本格式的數據字段的能力
* 文本格式流將由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;
}
// 接著寫入的是否最后一個字段
// 寫第一個字段前以參數false調用它
// 寫最后一個字段前以參數false調用它
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
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/173704
發表時間: 2008年03月19日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
我的一個日期處理類,解決了時區問題,給有需要的人。
package util;
/**
* --------------------------------------------------
* 日期轉換對象
* --------------------------------------------------
* 主要提供日期與1970-01-01后的天數的轉換和到字符串的轉換
* --------------------------------------------------
*
* @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; // 一天的微秒數
private static int rawOffset = TimeZone.getDefault().getRawOffset();
/**
* 將日期轉換為1970-01-01后的天數
*
* @param Date
* theDate 要計算天數的日期
* @return int 所傳入日期與1970-01-01相差的天數
*/
public static int dateToDay(Date theDate) {
return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
}
/**
* 將1970-01-01后的天數轉換為日期
*
* @param int
* 要取的日期與1970-01-01相差的天數
* @return Date theDate 與1970-01-01相差相應天數的日期
*/
public static Date dayToDate(int day) {
return new Date(day * MSEC_EVERYDAY);
}
/**
* 取今天與1970-01-01相差的天數
*
* @return int 取今天與1970-01-01相差的天數
*/
public static int toDay() {
return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
}
/**
* 將日期轉換為年月日字符串
*
* @param int
* theDay 與1970-01-01相差的天數
* @return String 對應日期年月日形式的字符串
*/
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);
}
/**
* 將日期轉換為年月字符串
*
* @param int
* theDay 與1970-01-01相差的天數
* @return String 對應日期年月形式的字符串
*/
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);
}
/**
* 將日期轉換為月日字符串
*
* @param int
* theDay 與1970-01-01相差的天數
* @return String 對應日期月日形式的字符串
*/
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);
}
/**
* 將日期轉換為當月一號
*
* @param int
* theDay 與1970-01-01相差的天數
* @return int 對應日期所在月份第一天與1970-01-01相差的天數
*/
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相差的天數
* @return int 對應日期所在年份
*/
public static int getYear(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.YEAR);
}
/**
* 取日期所在月份
*
* @param int
* theDay 與1970-01-01相差的天數
* @return int 對應日期所在月份
*/
public static int getMonth(int theDay) {
_calendar.setTime(dayToDate(theDay));
return _calendar.get(Calendar.MONTH);
}
/**
* 取日期所在周次
*
* @param int
* theDay 與1970-01-01相差的天數
* @return int 對應日期所在周次
*/
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
網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172974
發表時間: 2008年03月17日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
Action: 規定了與用戶交互的View可以觸發的動作,在某個View新建之后顯示之前,應先為其指定具體的Action,當用戶按下了相應的Command按鈕之后,View將該Command對應的Action發送到該View的Controller進行處理。
//
public class Action{
String name; // 名稱
Command command; // 命令
int code; // 代碼 (將由該View的傳遞到其Controller使用)
Item item; // 數據項
boolean defaultAction; // 是否是默認的Action
//...省略
}
請看View的基類的代碼節選
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是屏幕相關的命令
}
else
{
item.addCommand( command ); // 該Action是數據項相關的命令
if( action.isDefaultAction() )
{
item.setDefaultCommand( command );
}
}
}
//...省略
// 用戶按下相應的命令鍵后,觸發執行與其關聯的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按下了命令按鈕時觸發執行與其關聯的Action
public void commandAction( final Command command, final Item item )
{
if( !handleAction( command ) )
{
AbstractController.commandAction( this, command );
}
}
// 根據所觸發的命令查找關聯的Action,并新它發送到Controller進行處理
public boolean handleAction( final Command command )
{
if( activeActions.containsKey( command ) )
{
final Action action = (Action) activeActions.get( command );
// 以Action代碼為參數生成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