通常我們可以在代碼中直接使用常量值或者是專門定義一個放常量的類,例如下面的樣子:
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