在MIDP中没有提供J2SE平台的ResourceBundle类,因此我自己写了一个。并通过简单的MIDlet程序测试成功。主要的目的是为了解决常量定义的问题,如果把GUI中组件的title的等常量放到一个文件里面调试起来会更方便。 通常我们可以在代码中直接使用常量值或者是专门定义一个放常量的类,例如下面的样子: 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。这是正确的 //IndexMIDlet.java 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; /* * Created on 2004-6-26 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author P2800 * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class IndexMIDlet extends MIDlet { private ResourceBundle indexBundle; private Display display; private TextBox box; /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#startApp() */ protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub 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); } /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#pauseApp() */ protected void pauseApp() { // TODO Auto-generated method stub } /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#destroyApp(boolean) */ protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } } //ResourceBundle.java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Vector; /* * Created on 2004-6-26 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author P2800 * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ 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 
|