77058

Unwanted backslash added to file saved by java method

Question:

In my spring project, one of my service classes has this method to save a file named database.properties in disk:

public void create_properties(String maquina, String usuario, String senha) { System.out.println("create_properties"); Properties props = new Properties(); props.setProperty("jdbc.Classname", "org.postgresql.Driver"); props.setProperty("jdbc.url", "jdbc:postgresql://"+maquina+"/horario" ); props.setProperty("jdbc.user", usuario ); props.setProperty("jdbc.pass", senha ); props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); props.setProperty("hibernate.show_sql", "false"); props.setProperty("hibernate.hbm2ddl.auto", "validate"); FileOutputStream fos; try { fos = new FileOutputStream( "database.properties" ); props.store( fos, "propriedades" ); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

My problem is that the property jdbc:url should be something like that:

jdbc:postgresql://localhost:5432/horario

But what is being saved is this:

jdbc\:postgresql\://localhost\:5432/horario

Anyone can tell me how to avoid this backslashes to be included?

Answer1:

It's doing exactly the right thing - you're saving a <em>properties</em> file, which escapes things like colons using backslashes. From the <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#store-java.io.Writer-java.lang.String-" rel="nofollow">documentation for Properties.store</a>:

<blockquote>

Then 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. 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.

</blockquote>

If you load the properties file in using Properties.load, you'll get the original string back in the Properties object.

If you don't want to store the value in a properties file, use a Writer and just write the string directly.

Recommend

  • Why dsofile.dll still need Office Installation?
  • Different results when setting/getting System properties with GStrings in Groovy
  • Is there a limit on how long a cfquery with cfqueryparam can get?
  • Maven use Encrypted passwords in POM
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • Angular - How to access and replace innerHTML from a directive
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Yii2: Using Kartik Depdrop Widget?
  • insert a picture into database(sqlite) with java code. what should i do?
  • getelement by class name for clicking
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Spark (Scala) Writing (and reading) to local file system from driver
  • How to log in to an HTTPS website with Jsoup?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • pip in virtualenv gets ConnectTimeoutError
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • Filter strings with regex before casting to numeric
  • JPA flush vs commit
  • pyodbc doesn't report sql server error
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Jetty 9 HashLoginService
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • Scrapy recursive link crawler
  • Cannot connect to cassandra from Spark
  • Cross-Platform Protobuf Serialization
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Large data - storage and query
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF