Recent study J2ME Development, also talk about my string management of a solution.
The purpose of string management is to manage strings in a resource-managed format.
1. This system should be accessible through static methods;
2. The string is stored in the resource;
3. can easily switch various languages to achieve multi-language support;
4. A string resource has a name that accesses the resource by name;
5. The rule of the first name should be the class name + internal name.
The next question is what format the resource is stored in:
Logically speaking, XML is the ideal way, but considering the particularity of J2ME system, it is more suitable to use plain text.
Format is String-name String-value
The name and value are split between spaces or \ t, and the different strings are delimited by \ n.
Internal use of Hashtable storage.
Through the above analysis, you can write the following Stringmanager
Package Vmlinux.app
Import java.io.*;
import java.util.*;
public class Stringmanager {
String l Ang_;
String name_;
Hashtable Strings_;
Boolean loaded_;
Public Stringmanager (String Lang)
{
Lang_=lang
Loaded_=false;
}
public void Setlang (String lang)
{
if (Lang_==lang)
return;
Lang_=lang;
Loaded_=false;
}
public void Load (String name)
{
if (name==name_ && loaded_)
return;
if (strings_==null)
St Rings_=new Hashtable ();
Else
Strings_.clear ();
InputStream Is=this.getclass (). getResourceAsStream ("/strings/" +name+ "." +lang_);
Try
{
byte[] buf=new byte[1024];
int c=is.read (BUF);
StringBuffer o=new StringBuffer ();
String Key=null;
do{
String data=new string (buf,0,c);
int s=0;
for (int i=0;i
{
Char x=data.charat (i);
if (s==0 && (x== ' | | x== ' \ t '))
{
key=o.tostring ();
O.delete (0,o.length ());
S=1;
Else if (s==1 && x== ' \ n ')
{
if (key!=null && key.length () >0)
Strings_.put (Key, O.tostring ());
Key=null;
O.delete (0,o.length ());
S=0;
}
Else
O.append (x);
}
}while ((C=is.read (BUF)) >0);
if (key!=null && key.length () >0)
Strings_.put (key,o.tostring ());
Is.close ();
}
catch (Exception ex)
{
}
Name_=name;
Loaded_=true;
}
public string getString (string name)
{
Object o=strings_.get (name),
if (o==null)
return name;
Els E
Return (String) o;
}
}