Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

java - Properties file backslash and semicolon

I have a Java class to write/append into existing properties file. After appending, it's replacing all single backslash with double backslash and it places single backslash before every semicolon.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("text/html");
  PrintWriter out= response.getWriter();
  String systemPath=request.getParameter("SYSTEMPATH");
  String deployPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/DB.properties");
  InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/DB.properties");
  Properties prop = new Properties();
  prop.load(stream);
  prop.setProperty("Workspace", systemPath);
  File file = new File(deployPath);
  FileOutputStream fileOut = new FileOutputStream(file);
  prop.store(fileOut, "sample properties");
  fileOut.close();
}

Before appending:

Url=jdbc:oracle:thin:@//192.168.1.22:1521/

Workspace=D:RACHELSWAntivirus

after appending:

Url=jdbc:oracle:thin:@//192.168.1.22:1521/

Workspace=D:\RACHEL\SW\Antivirus

How to remove these extra backslashes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The properties file should have the extra backslashes to start with. In particular, without them you could end up with the wrong data, e.g. if you have d:foo ew that wouldn't mean what you expect it to.

The backslashes escape characters which are sensitive in properties files, basically. The colons are unnecessary (as they're not in the key) but they don't do any harm either. The doubling of backslashes for text is entirely beneficial.

This is documented in the Properties documentation - in particular, look at the store() method that you're calling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...