Based on MIDP Realization ResourceBundle class (turn j2medev.com)

xiaoxiao2021-03-06  92

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 the constant of the Title of the component in the GUI, it will be more convenient to debug it in a file. This paper is more important to tell about how to achieve a lost J2SE in J2ME. class.

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 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 (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 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 (); byterrayoutputstream 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 file content 0 = ming1 = java2 = hello3 = world4 = DIGITAL5 = Hahaha

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

New Post(0)