Do your own MIDP-based RESOURCEBUNDLE class

zhaozj2021-02-16  48

There is no RESourceBundle class for the J2SE platform in the MIDP, so I wrote one yourself. And successfully tested through a simple MIDlet program. The main purpose is to solve the problem of constant definitions, if you put it more convenient to put the constant of the Title of the component in the GUI to a file.

Usually we can use a constant value directly or specifically define a class of unusual amounts, such as the following: form mianform = new form ("worsst)); form mainform = new form (title.formtitle); first The situation is the least indispensable, and it is very troublesome to modify it. Below I provide a resourcebundle class, which has a constructor is the first parameter of the public resourcebundle (INT size) to specify the name of the file, the second parameter is how many options are prepared in the file, which can be set More than actual. The format of the file should be strictly in this kind. 0 = ming1 = java2 = hello3 = world4 = DIGITAL5 = Hahaha

The contents of the file to be analyzed and the reading portion of the key inside the Vector is achieved: private void readToVector () throws IOException {InputStream is = this.getInputStreamFromFile (); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int c = 0; 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 oException ("index error");}} baos.reset ();} else {baos.write (c);}} specific usage The method is such a try {resourcebundle indexbundle = new resourcebundle ("/ index EX.PROPERTIES ", 20);} catch (ioException e) {} string s = indexbundle.getstring (3); // Anything you want to get in Index.properties can find the correctness of this class, I Write a simple MIDlet program test success. Note that I use the Eclipse so put the file index.properties in the res directory (if you don't have a new yourself), the following is the code, the result is ultimately displaying Hello in TextBox. This is the correct //indexmidlet.javaimport 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.javaimport 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 vidinity () throws oException {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) .ndswith (String.Valueof (index))) {indexVector.addelement (S.Substring (i 1) .trim ()); Index ;} else {throw new oException ("index error");}} baos.reset ();} else {baos.write (c);}}}} // index.properties}} // index.properties0} // index.properties}} // index.properties}}}} // index.properties}}} // index.properties}} //

转载请注明原文地址:https://www.9cbs.com/read-19840.html

New Post(0)