网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.J2EE的Web和企业架构(WEA)设计.
.MIDlet Code signing certificat.
.J2ME性能优化之—优化方法探讨.
.J2EE、Linux稳固e-business架构.
.使用J2ME程序测试MIDlet的生命周.
.解决JFreeChart和一些JAVA程序在.
.使用SAAJ1.2发送和接收二进制Web.
.JavaMail快速入门-4.
.搭建OTA下载服务器.
.基于MIDP1.0实现通信录.
.发送PDF文件.
.JSP+JavaScript实现类似MSDNCSDN.
.通过J2ME程序做一个用户数据报协.
.J2ME中使用 Canvas 制作简单的游.
.用Ant发布应用程序到OC4J.
.WebService开发的层次.
.Jboss下配置EJB.
.网络编程:建立推拉门式菜单.
.基于Java的Web服务器工作原理(三.
.如何在鸡尾酒会上谈论Jini,J2EE.

基于MIDP实现ResourceBundle类

发表日期:2007-12-23


    在MIDP中没有提供J2SE平台的ResourceBundle类,因此我自己写了一个。并通过简单的MIDlet程序测试成功。主要的目的是为了解决常量定义的问题,如果把GUI中组件的title的等常量放到一个文件里面调试起来会更方便,本文更重要的目的是告诉读着如何实现在J2ME中遗失的J2SE的类。

    通常我们可以在代码中直接使用常量值或者是专门定义一个放常量的类,例如下面的样子:
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。这是正确的

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(indexBundle.getString(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 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

(出处:)


上一篇:分析MIDP低级事件处理机制 人气:600
下一篇:下载和显示PNG图片 人气:1103
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐