简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM