只要在把你所新建的 .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 功能