[英]storing Hexadecimal value in Java properties file without “\”
我正在嘗試在屬性文件中添加十六進制值,該值正在存儲中,但是我可以看到附加了“ \\” ,這是我不想要的,
test.properties
#
#Fri Jun 07 21:18:49 GMT+05:30 2013
test=fe\:fe
Java文件
public class PropertySave {
private static File s_file;
private static Properties s_properties = new Properties();
public static void main(String[] args) {
loadProperties();
s_properties.setProperty("test", "fe:fe");
saveProperties();
}
private static void saveProperties() {
FileOutputStream fout = null;
try {
fout = new FileOutputStream(s_file);
s_properties.store(fout, "");
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
} finally {
if (fout != null) {
try {
fout.close();
} catch (final IOException ioe2) {
ioe2.printStackTrace();
System.exit(1);
}
}
}
}
private static void loadProperties() {
s_file = new File("test.properties");
if ((!s_file.exists()) || (!s_file.isFile())) {
System.exit(1);
}
FileInputStream fin = null;
try {
fin = new FileInputStream(s_file);
s_properties.load(fin);
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
} finally {
if (fin != null) {
try {
fin.close();
} catch (final IOException ioe2) {
ioe2.printStackTrace();
System.exit(1);
}
}
}
}
}
在Java文件中, s_properties.setProperty(“ test”,“ fe:fe”); 屬性文件中的輸出是不同的(test.properties) fe:fe這是我要忽略的,因為此屬性文件以“ C”語言輸入到其他系統,因此我的工作不正常,
我應該怎么做才能確保Java文件中的輸入和屬性文件中的輸出相同
考慮以已知方式對十六進制值進行編碼,然后在C和Java中對其進行解碼。
一種技術是在十六進制值之前添加“ 0x”,並將所有大寫字母用作數字。 如果使用此技術,則將需要某種方式來表示十六進制數的結尾。 我建議使用空格字符('')或行尾。
使用此技術,您的財產將如下所示:
test=0xFEFE
字符串“ Blam07kapow”(其中07是十六進制數字)將如下所示:
Blam0x07 kapow
Properties類使用前面的\\存儲:,以確保當您使用load方法時它將再次加載正確的值。 如果您以后希望能夠將此屬性文件重新加載到Java中,那么恐怕您會被屬性文件中的轉義字符所困擾。 請參閱java.util.Properties文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.