uu[4] = toHex((c >> 4) & 0xf);
uu[5] = toHex( c & 0xf);
bw.write(new String(uu));
} else {
bw.newLine();
if (c == '\r' &&
current != len - 1 &&
comments.charAt(current + 1) == '\n') {
current++;
}
if (current == len - 1 ||
(comments.charAt(current + 1) != '#' &&
comments.charAt(current + 1) != '!'))
bw.write("#");
}
last = current + 1;
}
current++;
}
if (last != current)
bw.write(comments.substring(last, current));
bw.newLine();
}
/*
* !!! Copy from java source code.
*/
private static char toHex(int nibble) {
return hexDigit[(nibble & 0xF)];
} www.2cto.com
/** A table of hex digits */
private static final char[] hexDigit = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
}
[java]
主要是实现两个功能:
1.顺序输出:
用户通过使用方法setP来插入数据,三个参数分别为key,value,和comment。之后通过orderStore来输出内容。
2.为每条属性添加注释
通过setP方法添加的属性都会有注释参数。如果只想使用注释这个功能,可以通过java的Properties类来构造此类对象,之后通过addComment方法来为想要添加的属性添加注释
测试程序:
[java]
public static void main(String[] args) throws IOException
{
OutputOrderProperties properties = new OutputOrderProperties();
FileOutputStream fileOutputStream = new FileOutputStream("test.properties");
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream);
for(int i=0 ; i<10; i++)
{
String string = String.valueOf(i);
properties.setP("Name"+string, string, "the name of "+string);
}
properties.orderStore(writer, "This is a test process...");
}
结果:
[plain]
#This is a test process...
#Wed Mar 13 15:34:03 CST 2013
#the name of 0
Name0=0
#the name of 1
Name1=1
#the name of 2
Name2=2
#the name of 3
Name3=3
#the name of 4
Name4=4
#the name of 5
Name5=5
#the name of 6
Name6=6
#the name of 7
Name7=7
#the name of 8
Name8=8
#the name of 9
Name9=9