日期:2014-05-20  浏览次数:20830 次

j2me读取中文的配置文件
还可以参考这篇文章:
http://gundumw100.iteye.com/blog/734885
import java.io.InputStream;
import java.util.Hashtable;
/**
* Task: 可以读取配置文件,// 为注释行
* @author JAROD
*
*/
public class TxtReader
{
        

        private  Hashtable hashtable; // 系统的一个词典
        
        public  void loadTxt(String nameP)
        {

                InputStream is = Resources.getInputStream(nameP);
                if(is == null)
                {
                        System.out.println(nameP+"没有找到");
                        return;
                }
                try
                {
                        String line;
                        hashtable = new Hashtable();
                        while (true)
                        {
                                line = getLine(is);
                                if(line == null)
                                {
                                        break;
                                }
                                if(line.length()>2 & line.charAt(0) != '/')
                                {
                                        int split = line.indexOf("=");
                                        if(split != -1)
                                        {
                                                String key = line.substring(0, split);
                                                String value = line.substring(split + 1, line.length());
                                                key = key.trim();
                                                value = value.trim();
                                                hashtable.put(key, value);
                                        }
                                }
                        }
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
               
        }
        
        private String getLine(InputStream is)
        {
                int nextByte = 0;
                byte[] byteArray = new byte[1024];
                int count = 0;
                while(true)
                {
                  try
                  {
                   nextByte = is.read();
                   byteArray[count]=(byte)nextByte;
                   ++count;
                  
                  }
                  catch(Exception e)
                  {
                        e.printStackTrace();
                  }
                 
                  if(nextByte == '\n')
                  {
                        
                          try
                          {
                            return new String(byteArray,0,count,"UTF-8");
                          }
                          catch(Exception e)
                          {
                                  e.printStackTrace();
                          }
                  }
                  if(nextByte == -1)
                  {
                          return null;
                  }
                }
               
        }
        
        public int getIntVal(String key, int defval)
        {
                String val = (String) hashtable.get(key);
                if(val == null)
                {
                        return defval;
                }
                return Integer.parseInt(val);
        }
        
        public String getStringVal(String key, String defval)
        {
                String val = (String) hashtable.get(key);
                if(val == null)
                {
                        return defval;
                }
                return val;
        }
}


使用示例:
TxtReader reader = new TxtReader();
reader.loadTxt("/config.txt");
int width = reader.getIntVal("SCREEN_WIDTH",0);
String musicFomat = reader.getStringVal("MUSIC_FOMAT","");

config.txt 中的内容:

SCREEN_WIDTH = 240
SCREEN_HEIGHT = 360
MUSIC_FOMAT=audio/midi


注意:如果你要使用中文,那麽,在存储时,要存储成UTF-8格式 。