Java - Writing file location to Properties file

后端 未结 3 600
谎友^
谎友^ 2021-01-23 04:17

I\'m writing the FILEPATH parameter into the properties file like below..

String newFilePath = txtFilepath.getText();
Properties prop = new Prop         


        
相关标签:
3条回答
  • 2021-01-23 04:47

    Backslash \ is an escape character that is silently dropped in not followed by another \

    Colon : is a special character that must by escaped with \.

    Check docs at

    http://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

    0 讨论(0)
  • 2021-01-23 04:56

    Just call prop.get("FILEPATH"). You will see that you get back "D:\filelog.txt", just as you put it in.

    0 讨论(0)
  • 2021-01-23 04:59

    In a properties file, : has a meaning.

    key = value
    key: value
    

    both are valid. Hence, it has to be escaped while writing to a properties file. Even \ needs escaping. But when you display them on the console, the escaping is identified, and the values are shown properly.

    If you see the docs of store() method of the Properties class

    Every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. Each character of the key and element strings is examined to see whether it should be rendered as an escape sequence. The ASCII characters \, tab, form feed, newline, and carriage return are written as \, \t, \f \n, and \r, respectively. Characters less than \u0020 and characters greater than \u007E are written as \uxxxx for the appropriate hexadecimal value xxxx. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

    Note:- It shouldn't be a problem if you use Properties to read the properties file(it will handle the escaping), but if you read the properties file like a normal text file(you should never do it unless you're out of your mind), then it'll be a problem.

    0 讨论(0)
提交回复
热议问题