`
muscle-liu
  • 浏览: 230578 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

j2me 实现 j2se 的 Properties 功能

    博客分类:
  • j2me
阅读更多
我们知道 j2me 中没有 j2se 里边的 Properties 类,要自己实现才能像 j2se 那样读取文件的,现在 j2mepolish 里边的 de.enough.polish.util.Properties 就实现了类似 j2se 的 Properties, 加上de.enough.polish.util.ResourceStreamUtil(旧版本polish 没有这个类,要自己实现相应功能)可以让我们容易读取属性文件。关于 ResourceStreamUtil 类和 Properties 类的介绍大家可以参考 j2mepolish 里边的 api doc。

只要在把你所新建的 .properties 文件放在 resources 文件夹下(相当于一个资源文件,也可以放其他地方,但一定要把它配置成资源),就可以用相应的方法读取, 看下边例子:

test.properties文件:
name=muscle-liu
sex=male
age=24


读取时的部分代码:
Properties prop = new Properties();
InputStream in = null;
try{
  byte[] sData = ResourceStreamUtil.getResourceAsByteArray("/test.properties");
  System.out.println("sData.length:  "+sData.length);
  in =  new ByteArrayInputStream(sData);
            
  prop.load(in);
}catch (IOException e){
  e.printStackTrace();
}

System.out.println("prop.getProperty(\"name\"): "+prop.getProperty("name"));
System.out.println("prop.getProperty(\"sex\"): "+prop.getProperty("sex"));
System.out.println("prop.getProperty(\"age\"): "+prop.getProperty("age"));


运行结果如下:
prop.getProperty("name"): muscle-liu
prop.getProperty("sex"): male
prop.getProperty("age"): 24


在运用中我发现这个 Properties 类有个 bug,就是不支持 # 的注释(也就是说,属性文件里边除了 key-value 的内容,不能有其他的非 k-v 内容)。下边是我修改 Properties 里边的 load 方法,令它支持 # 注释:
原来的 load 方法:
public void load(InputStream in, String encoding, boolean generateIntegerValues ) throws IOException
	{
		this.isIntegerValues = generateIntegerValues;
		int bufferLength = 2 * 1024;
		byte[] buffer = new byte[ bufferLength ];
		int read;
		int start = 0;
		int end = 0;
		boolean newLineFound;
		while ( (read = in.read(buffer, start, bufferLength - start )) != -1) {
			// search for next \r or \n
			String line;
			if (encoding != null) {
				line = new String( buffer, 0, read + start, encoding );
			} else {
				line = new String( buffer, 0, read + start );				
			}
			start = 0;
			newLineFound = true;
			while (newLineFound) {
				newLineFound = false;
				char c = '\n';
				for (int i = start; i < line.length(); i++) {
					c = line.charAt(i);
					if (c == '\r' || c == '\n') {
						end = i;
						newLineFound = true;
						break;
					}
				}
				if (newLineFound) {
					int splitPos = line.indexOf('=', start);
					if(splitPos == -1) {
						throw new IOException("no = separator: " + line.substring( start, end ));
					}
					String key = line.substring( start, splitPos );
					String value = line.substring( splitPos + 1, end );
					if (generateIntegerValues) {
						try {
							put( key, Integer.valueOf(value) );
						} catch(NumberFormatException ex) {
							throw new IOException( ex.toString() );
						}												
					} else {
						put( key, value );	
					}
					if (c == '\r') {
						start = end + 2;
					} else {
						start = end + 1;
					}
				}
			}
			// now all key-value pairs have been read, now move any remaining data to the beginning of the buffer:
			if (start < read) {
				System.arraycopy( buffer, start, buffer, 0, read - start );
				start = read - start;
			} else {
				start = 0;
			}
		}
	}	



修改后的 load 方法:
public void load(InputStream in, String encoding, boolean generateIntegerValues ) throws IOException
	{
		this.isIntegerValues = generateIntegerValues;
		int bufferLength = 2 * 1024;
		byte[] buffer = new byte[ bufferLength ];
		int read;
		int start = 0;
		int end = 0;
		boolean newLineFound;
		boolean isComment;
		while ( (read = in.read(buffer, start, bufferLength - start )) != -1) {
			// search for next \r or \n
			String line;
			if (encoding != null) {
				line = new String( buffer, 0, read + start, encoding );
			} else {
				line = new String( buffer, 0, read + start );				
			}
			start = 0;
			newLineFound = true;
			while (newLineFound) {
				newLineFound = false;
				isComment = false;
				char c = '\n';

				char firstChar = line.charAt(start);
				if(firstChar == '#'){
					isComment = true;
				}

				for (int i = start; i < line.length(); i++) {
					c = line.charAt(i);
					
					if (c == '\r' || c == '\n') {
						end = i;
						newLineFound = true;
						break;
					}
				}
				if (newLineFound && !isComment) {
					int splitPos = line.indexOf('=', start);
					if(splitPos == -1) {
						throw new IOException("no = separator: " + line.substring( start, end ));
					}
					String key = line.substring( start, splitPos );
					String value = line.substring( splitPos + 1, end );
					if (generateIntegerValues) {
						try {
							put( key, Integer.valueOf(value) );
						} catch(NumberFormatException ex) {
							throw new IOException( ex.toString() );
						}												
					} else {
						put( key, value );	
					}
				}

				if (c == '\r') {
					start = end + 2;
				} else {
					start = end + 1;
				}
			}
			// now all key-value pairs have been read, now move any remaining data to the beginning of the buffer:
			if (start < read) {
				System.arraycopy( buffer, start, buffer, 0, read - start );
				start = read - start;
			} else {
				start = 0;
			}
		}
	}


这样我们就可以像 j2se 那样写 properties 文件了..当然,如果你的项目中没有用 j2mepolish, 那你也可以借用这两个类到你的工程,照样可以实现 j2se 的 properties 功能
  • Properties.rar (3.6 KB)
  • 描述: 修改过的 properties 类和 ResourceStreamUtil类
  • 下载次数: 186
分享到:
评论
3 楼 muscle-liu 2008-07-30  
我们可以继承j2me里边的Hashtable来实现的,而接口Externalizable 是继承Serializable的,所以完全可以改为自己的properties,在其他的j2me项目里都能用
2 楼 jandyguan 2008-07-15  
因为Properties类继承Hashmap,实现接口Externalizable,这些都是Polish的东西,所以在其它项目单用这两个类是不行的。
1 楼 mingkg21 2008-04-14  
复杂了一些,还可以简单一点。。。。。

相关推荐

    提供j2me使用的优化过的Properties源码

    描述中指出,这个自写的Properties类实现了与J2SE中Properties类类似的功能。这可能包括读取和写入配置文件,支持键值对的存取,以及可能的编码转换等功能。开发者可能还考虑到了J2ME的特殊环境,例如文件I/O操作的...

    polish.pdf

    这通常通过配置文件或项目构建工具完成,例如`build.properties`文件,它包含了关于项目的各种配置属性。 3. **预处理**: 预处理是编译过程的一部分,用于在代码实际编译之前进行文本替换、条件编译等操作。在J2ME ...

    Derby数据库的使用指南--包括存图片到数据库和读取数据库中的图片操作

    环境:Windows XP professional, JDK 1.6, Eclipse 3.3 安装JDK 6.0之后会安装自带的纯Java的数据derby. 也就是说,如果安装JDK 6.0版本...该数据数应该为J2ME编程带来非常大的好处,同时也为J2SE和J2EE编程来带方便。

    IBM JAVA培训计划

    - **J2ME**: Java微版,适用于移动设备等资源受限环境。 ##### 2. Java基础 - 数据类型与表达式 - 数组 - 继承、重载、覆盖 - 静态与抽象 - 接口与实现 - 内部类 - 集合与遍历 - 日期处理 - Properties、File、...

    我的所有 JAVA 项目.zip

    Java涵盖了众多子领域,如J2SE(Java标准版)用于桌面应用,J2EE(Java企业版)用于企业级应用开发,J2ME(Java微型版)则针对嵌入式设备。因此,这些项目可能涉及Web开发(使用Spring Boot、Struts等框架),桌面...

    java培训总结

    - **J2EE (Java 2 Platform, Enterprise Edition)**: 面向企业级应用开发的平台,包含了J2SE中的所有功能及更多面向服务端的功能。 - **J2ME (Java 2 Platform, Micro Edition)**: 针对嵌入式设备和移动设备的平台。...

    JAVA编程基础教程.pptx

    * Java Standard Edition(J2SE):用于开发客户端独立应用程序或Applet。 * Java Enterprise Edition(J2EE):用于开发服务器端应用程序,如Java Servlet和Java Server Pages。 * Java Micro Edition(J2ME):...

Global site tag (gtag.js) - Google Analytics