日期:2014-05-20 浏览次数:21130 次
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
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;
}
}
}
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;
}
}
}