如一文件(假设是a.properties)中有以下数据, 并假设并不知道key
a=hello
b=tomorrow
c=fuck\theWord
通过以下方法load properties, 读取它的值:
Properties p=new Properties();
p.load(new FileInputStream("a.properties"))Enumeration<Object> elements = p.elements();
while(elements.hasMoreElements){
System.out.println(elements.nextElement())
}现在问题来了,为什么第3个数据变成了fucktheWord, 而斜杠却消失了?
而我将第3个数据改成c=fuck\\theWord就可以将一个斜杠读进来,是不是Properties将斜杠解释成转义字符而忽略它了呢?
请教各位指教!谢谢。
a=hello
b=tomorrow
c=fuck\theWord
通过以下方法load properties, 读取它的值:
Properties p=new Properties();
p.load(new FileInputStream("a.properties"))Enumeration<Object> elements = p.elements();
while(elements.hasMoreElements){
System.out.println(elements.nextElement())
}现在问题来了,为什么第3个数据变成了fucktheWord, 而斜杠却消失了?
而我将第3个数据改成c=fuck\\theWord就可以将一个斜杠读进来,是不是Properties将斜杠解释成转义字符而忽略它了呢?
请教各位指教!谢谢。
如何才能将\请出来?
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Reading and Writing a Properties File
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
"infilename"));
} catch (IOException e) {
}
// Write properties file.
try {
properties.store(new FileOutputStream(
"outfilename"), null);
} catch (IOException e) {
}
Here is an example of the contents of a properties file:
# a comment
! a comment
a = a string
b = a string with escape sequences \t
\n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \
contination line
d.e.f = another string
Examplets TM provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon