Reading Java Properties file without escaping values

前端 未结 8 1794
滥情空心
滥情空心 2020-12-06 09:54

My application needs to use a .properties file for configuration. In the properties files, users are allow to specify paths.

Problem

Propert

8条回答
  •  一向
    一向 (楼主)
    2020-12-06 10:13

    @pdeva: one more solution

    //Reads entire file in a String 
    //available in java1.5
    Scanner scan = new Scanner(new File("C:/workspace/Test/src/myfile.properties"));   
    scan.useDelimiter("\\Z");   
    String content = scan.next();
    
    //Use apache StringEscapeUtils.escapeJava() method to escape java characters
    ByteArrayInputStream bi=new ByteArrayInputStream(StringEscapeUtils.escapeJava(content).getBytes());
    
    //load properties file
    Properties properties = new Properties(); 
    properties.load(bi);
    

提交回复
热议问题