J2ME 技術的學習與實踐者

          2008年3月25日 #

          j2me如何讀取網上資源文件例如文本文件,圖形文件,歡迎投稿!

          j2me如何讀取網上資源文件例如文本文件,圖形文件。

          例如,讀取www.kingdart.cn/jaccount/imobile.png 轉換為Image
          又例如:讀取www.kingdart.cn/jaccount/readme.txt 轉換為String

          只在模擬器上成功我也會,要求是真機上成功!

          posted @ 2008-03-25 22:56 iwinyeah 閱讀(627) | 評論 (1)編輯 收藏

          2008年3月22日 #

          [導入]WTK模擬器之RMS(5 還是有可能在手機上做出文件式RMS的)


          網站: 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

          posted @ 2008-03-22 17:01 iwinyeah 閱讀(602) | 評論 (3)編輯 收藏

          [導入]FileConnection如何使用?


          網站: 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

          posted @ 2008-03-22 10:55 iwinyeah 閱讀(331) | 評論 (0)編輯 收藏

          2008年3月21日 #

          [導入]字段輸入流FieldInuptStream


          網站: 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;
          	}
          }
          

          請參看我的另一篇文章:字段輸出流FieldOutputStreamhttp://iwinyeah.javaeye.com/admin/blogs/174644
          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://iwinyeah.javaeye.com/blog/174645

          posted @ 2008-03-21 22:19 iwinyeah 閱讀(175) | 評論 (0)編輯 收藏

          [導入]字段輸出流FieldOutputStream


          網站: 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

          posted @ 2008-03-21 22:16 iwinyeah 閱讀(214) | 評論 (0)編輯 收藏

          2008年3月19日 #

          [導入]日期處理類(忽略時間)


          網站: 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

          posted @ 2008-03-19 12:32 iwinyeah 閱讀(220) | 評論 (0)編輯 收藏

          2008年3月17日 #

          [導入]OpenBaseMovil Action <--> View <--> Controller


          網站: 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

          posted @ 2008-03-17 14:06 iwinyeah 閱讀(351) | 評論 (0)編輯 收藏

          2008年3月15日 #

          [導入]Nokia 6070 報表問題解決


          網站: JavaEye  作者: iwinyeah  鏈接:http://iwinyeah.javaeye.com/blog/172237  發表時間: 2008年03月15日

          聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!

          經過多次的嘗試,終于解決了手機理財JAccount在Nokia 6070反復統計收支表和余額表時出錯的問題.
          原來我有兩個報表上分別使用了incomeVector 和balanceVector來保存所生成的統計資料,每次統計前檢查Vector是否為null,否則先置空,再重新new一個.
          我嘗試過new之后加了runtime.gc(),未能解決問題;
          我又嘗試過不置空Vector,而使用vector.removeallelements(),也不行;
          我又嘗試過兩個報表共用一個Vector也不行;

          最后,我使用兩個報表共用數組來保存結果,才解決了問題,有點開心.
          類似Nokia6070這種機器的JVM的內存管理的確存在很大的問題,明明有內存也用不得,真郁悶!
          不過還是有點開心,畢竟解決了一個問題!
          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://iwinyeah.javaeye.com/blog/172237

          posted @ 2008-03-15 21:43 iwinyeah 閱讀(152) | 評論 (0)編輯 收藏

          [導入]我在J2ME中用過的幾種后臺線程方法(如何選擇?)


          網站: JavaEye  作者: iwinyeah  鏈接:http://iwinyeah.javaeye.com/blog/172200  發表時間: 2008年03月15日

          聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!

          我以前在其他地方發過的貼子,這幾種方式都沒問題,哪種較好或者說在什么情況下用哪種方法較好呢?
          // 方法一
          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

          posted @ 2008-03-15 17:47 iwinyeah 閱讀(203) | 評論 (0)編輯 收藏

          2008年3月12日 #

          [導入]OpenBaseMovil StreamParser 流標識符分段器


          網站: JavaEye  作者: iwinyeah  鏈接:http://iwinyeah.javaeye.com/blog/170335  發表時間: 2008年03月12日

          聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!

          在處理資源文件時,我以前的做法是一次性讀入資源文件,然后再進行處理,在處理大文件時,這種方法對由于對機器內存消耗較大而存在隱患,剛想將它改為逐字讀入的方式,在OpenBaseMovil中發現了這個類,很符合我的要求。關鍵代碼如下:
          //... 省略
          	public static final String WHITESPACE = "\r\n\t ";
          	public String next(final String delimiters, final boolean keepWhitespace,
          			final boolean allowComments, final boolean reuseDelimiter,
          			final boolean processEscape) throws IOException {
          		try {
          			final StringBuffer token = new StringBuffer();
          			startLine = endLine;
          			startChar = endChar;
          			int c = in.read();
          			endChar++;
          			int status = INITIAL;
          			while (c != -1) { // 若還未讀到文件尾
          				if (c == '\n') {
          					endLine++;
          					endChar = 0;
          				}
          				switch (status) {
          				case INITIAL:
          					if (delimiters.indexOf(c) > -1) { // 如果是分隔符
          						lastDelimiter = (char) c;
          						if (isWhiteSpace(c)) {
          							// 如果同時也是空白符并且標識符長度大于零則返回標識符
          							if (token.length() > 0) {
          								if (reuseDelimiter) { // 如果要重用分隔符則將它推回輸入流中
          									in.revert((char) c);
          								}
          								return token.toString();
          							}
          							// 如果還未有數據,還要繼續往下讀
          						} else { // 如果不是空白符則無論標識符長度是否為零,都要返回
          							if (reuseDelimiter) {
          								in.revert((char) c);
          							}
          							return token.toString();
          						}
          					} else if (processEscape && c == '\\') {
          						status = ESCAPE; // 設轉義字符標志
          					} else if (allowComments && c == '/') {
          						status = COMMENT_START; // 設注釋標志
          					} else if (isWhiteSpace(c)) {
          						if (keepWhitespace) { // 如果空白符也要用,把它加入標識符中
          							token.append((char) c);
          						}
          					} else {
          						token.append((char) c);
          					}
          					break;
          
          				case ESCAPE: // 處理轉義字符
          					switch (c) {
          					case 'n':
          						token.append('\n');
          						break;
          
          					case 'r':
          						token.append('\r');
          						break;
          
          					case 't':
          						token.append('\t');
          						break;
          
          					case 'b':
          						token.append('\b');
          						break;
          
          					case 'f':
          						token.append('\f');
          						break;
          
          					default:
          						token.append((char) c);
          						break;
          					}
          					status = INITIAL; // 設正常情況標志
          					break;
          
          				case COMMENT_START: // 處理注釋
          					if (c == '/') {
          						status = LINE_COMMENT; // 是行式注釋
          					} else if (c == '*') {
          						status = BLOCK_COMMENT; // 是塊式注釋
          					} else {
          						status = INITIAL;
          						// 如果都不是則把注釋起始符和剛讀入的字符都加入到標識符中
          						token.append('/').append((char) c);
          					}
          					break;
          
          				case LINE_COMMENT:
          					if (c == '\n') {
          						status = INITIAL; // 如果當前為行注釋狀態則要一直讀到行尾才恢復正常情況標志
          					}
          					break;
          
          				case BLOCK_COMMENT:
          					if (c == '*') {
          						status = COMMENT_END; // 如果當前為塊注釋狀態則要一直讀到*號設為塊注釋結束狀態
          					}
          					break;
          
          				case COMMENT_END:
          					if (c == '/') {
          						status = INITIAL; // 在塊結束狀態下讀到/則為塊結束
          					} else {
          						status = BLOCK_COMMENT; // 否則塊注釋還未結束,恢復為塊注釋狀態
          					}
          					break;
          
          				}
          				c = in.read(); // 讀入下一字符
          			}
          			// 如果讀到文件尾時,標識符長度大于零,則返回標識符,否則返回NULL值
          			return token.length() > 0 ? token.toString() : null;
          		} catch (IOException e) {
          			throw new IOException("Error reading input L=" + startLine + " C="
          					+ startChar);
          		}
          	}
          //... 省略
          


          不過從代碼可以看出,它并不支持非Ascii編碼格式的文件,還要進行進一步的改造。
          我的計劃是StringBuffer 用byte[]代替,增加setEncode(String encode)方法,返回字符串時使用 new String(byte[], encode)
          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://iwinyeah.javaeye.com/blog/170335

          posted @ 2008-03-12 07:03 iwinyeah 閱讀(219) | 評論 (0)編輯 收藏

          僅列出標題  下一頁
          主站蜘蛛池模板: 曲水县| 简阳市| 祥云县| 南汇区| 武平县| 突泉县| 新乐市| 阳曲县| 淮南市| 卓资县| 彩票| 望奎县| 中江县| 荣昌县| 彝良县| 新闻| 香港| 平山县| 丹巴县| 当涂县| 积石山| 凤庆县| 宿州市| 台北县| 旬邑县| 渑池县| 桃园县| 昆山市| 思南县| 阿尔山市| 高雄县| 临朐县| 聂荣县| 健康| 滦南县| 吉林省| 凌源市| 齐齐哈尔市| 乐业县| 云梦县| 宜章县|