user54517
user54517

Reputation: 2420

Java System.getProperty("user.home")

In this line of code, I am using the getProperty method :

PrintWriter writer = new PrintWriter("~/4413/ctrl/geo.txt".replaceFirst("^~", System.getProperty("user.home")), "UTF-8");

The result is C:UsersKamel\4413\ctrl\geo.txt which not exactly what I want as long as some \ separators are missing at the begining.

Is there anything I can add so that the path becomes correct ?

Upvotes: 0

Views: 161

Answers (1)

Andy Turner
Andy Turner

Reputation: 140309

Quote System.getProperty("user.home"):

Matcher.quoteReplacement(System.getProperty("user.home"))

The issue is that both parameters of replaceFirst treat characters in special ways, because you are dealing with regular expressions, not literal strings.

Upvotes: 3

Related Questions