浏览 3623 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-07
只要在把你所新建的 .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 功能 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-07-15
因为Properties类继承Hashmap,实现接口Externalizable,这些都是Polish的东西,所以在其它项目单用这两个类是不行的。
|
|
返回顶楼 | |
发表时间:2008-07-30
我们可以继承j2me里边的Hashtable来实现的,而接口Externalizable 是继承Serializable的,所以完全可以改为自己的properties,在其他的j2me项目里都能用
|
|
返回顶楼 | |