国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

基于MIDP實現ResourceBundle類

2019-11-18 16:07:56
字體:
來源:轉載
供稿:網友
    在MIDP中沒有提供J2SE平臺的ResourceBundle類,因此我自己寫了一個。并通過簡單的MIDlet程序測試成功。主要的目的是為了解決常量定義的問題,如果把GUI中組件的title的等常量放到一個文件里面調試起來會更方便,本文更重要的目的是告訴讀著如何實現在J2ME中遺失的J2SE的類。

    通常我們可以在代碼中直接使用常量值或者是專門定義一個放常量的類,例如下面的樣子:
Form mianForm = new Form("最差"); Form mainForm = new Form(Title.FORMTITLE);第一種情況是最不可取的,如果修改起來很麻煩。下面我提供了一個 ResourceBundle類,它有一個構造器是
public ResourceBundle(String fileName,int size)第一個參數來指定文件的名字,第二個參數是文件中準備存儲多少個選項,一般可以設置的比實際大一些。文件的格式應該是嚴格按照這樣的樣子。
0=ming
1=java
2=hello
3=world
4=digital
5=hahaha

把文件的內容進行分析并讀取到Vector里面的關鍵部分是這樣實現的:
PRivate void readToVector() throws IOException
    {
        InputStream is = this.getInputStreamFromFile();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = 0;
        int index = 0;
        while ((c = is.read()) != -1)
        {
            if (c == '/n' c == '/r')
            {
                String s = baos.toString();
                int i = s.indexOf('=');
                if (i != -1)
                {
                    if (s.substring(0, i).endsWith(String.valueOf(index)))
                    {
                        indexVector.addElement(s.substring(i + 1).trim());
                        index++;
                    } else
                    {
                        throw new IOException("index error");
                    }
                }
                baos.reset();
            } else
            {
                baos.write(c);
            }
        }
具體的使用方法是這樣的
try
{
   ResourceBundle indexBundle = new ResourceBundle("/index.properties",20);
}
catch(IOException e)
{}
String s = indexBundle.getString(3);//任何你想得到的在index.properties中可以找到的title
為了測試這個類的正確性,我寫了一個簡單的MIDlet程序測試成功。注意我是用的Eclipse因此把文件index.properties是放在res目錄里面(如果沒有可以自己新建)下面是代碼,運行結果是最終在textbox里面顯示hello。這是正確的

import java.io.IOException;


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class IndexMIDlet extends MIDlet
{
    private ResourceBundle indexBundle;
    private Display display;
    private TextBox box;
    
    
    protected void startApp() throws MIDletStateChangeException
    {
        display = Display.getDisplay(this);
        try
        {
            indexBundle = new ResourceBundle("/index.properties",15);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        box = new TextBox("IndexBundle",null,256,TextField.ANY);
        box.setString(indexBundle.getString(2));
        display.setCurrent(box);
       
    }

   
    protected void pauseApp()
    {
    }

  
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {
    }

}

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;


public class ResourceBundle
{
    private Vector indexVector;
    private String fileName;
  
    private ResourceBundle()
    {
    }

    public ResourceBundle(String fileName, int size) throws IOException
    {
        this.fileName = fileName;
        indexVector = new Vector(size);
        init();
    }

    private InputStream getInputStreamFromFile()
    {
        return new ResourceBundle().getClass().getResourceAsStream(
                fileName);
    }


    private void init() throws IOException
    {
        readToVector();
    }

    public String getString(int indexID)
    {
        if (indexID < 0 indexID > indexVector.size())
        {
            return null;
        } else
        {
            return (String) indexVector.elementAt(indexID);
        }
    }

    private void readToVector() throws IOException
    {
        InputStream is = this.getInputStreamFromFile();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = 0;
        int index = 0;
        while ((c = is.read()) != -1)
        {
            if (c == '/n' c == '/r')
            {
                String s = baos.toString();
                int i = s.indexOf('=');
                if (i != -1)
                {
                    if (s.substring(0, i).endsWith(String.valueOf(index)))
                    {
                        indexVector.addElement(s.substring(i + 1).trim());
                        index++;
                    } else
                    {
                        throw new IOException("index error");
                    }
                }
                baos.reset();
            } else
            {
                baos.write(c);
            }
        }
    }


}
index.properties文件內容
0=ming
1=java
2=hello
3=world
4=digital
5=hahaha

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霞浦县| 信丰县| 登封市| 黎平县| 突泉县| 西安市| 珲春市| 大城县| 北宁市| 苍梧县| 班戈县| 措美县| 湘西| 新营市| 怀远县| 万安县| 临武县| 乐昌市| 铅山县| 湖北省| 房产| 壤塘县| 江津市| 深州市| 庆安县| 麻城市| 贞丰县| 乐东| 精河县| 长兴县| 屯昌县| 平和县| 茂名市| 开平市| 阳曲县| 错那县| 柞水县| 高淳县| 宁安市| 怀远县| 土默特右旗|