Home > Java > javaTutorial > body text

How to write backslash \ in Java's .properties file?

PHPz
Release: 2023-04-23 16:01:07
forward
1648 people have browsed it

Question

My project is an ssh project. I need to upload a file, and then the file path needs to read the properties configuration
There is config/application.properties under resource

How to write backslash \ in Javas .properties file?

Then the tool class is written like this, this can be used

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
public class PropertiesUtil {
  private static Properties props = new Properties();
  private static PropertiesUtil instances = null;
  private static String NAME = "config//application";
  public static PropertiesUtil getInstance() {
    if (null == instances) {
      instances = new PropertiesUtil();
    }
    return instances;
  }
  private PropertiesUtil() {
    init(NAME);
  public synchronized void init(String sPropFilePathName) throws MissingResourceException {
    String propFile = sPropFilePathName;
    ResourceBundle bundle = ResourceBundle.getBundle(propFile);
    Enumeration enume = bundle.getKeys();
    Object key = null;
    Object value = null;
    while (enume.hasMoreElements()) {
      key = enume.nextElement();
      value = bundle.getString(key.toString());
      props.put(key, value);
  public String getProperty(String key) {
    return props.getProperty(key);
  
  public static String getValue(String filePath, String key)
	{
		InputStream in = null;
		String value = null;
		try
		{
			in = PropertiesUtil.class.getResourceAsStream(filePath);
			props.load(in);
			value = props.getProperty(key);
		}
		catch (Exception e)
			e.printStackTrace();
		}finally{
			try
			{
				if(in != null) {
					in.close();
				}
			}
			catch (IOException e)
				e.printStackTrace();
		return value;
	}
  public static void main(String[] args) {
    System.out.println(PropertiesUtil.getInstance().getProperty("属性key"));
}
Copy after login

If I write the properties as follows

How to write backslash \ in Javas .properties file?

The project is directly Can't start, reported error

Solution

After research, using "\" in properties is equivalent to the escape character of java
If you want to write out the effect of \, just modify it You can write it as follows

How to write backslash \ in Javas .properties file?

#Then the project is started, and then the path inserted into the database is normal~

How to write backslash \ in Javas .properties file?

The above is the detailed content of How to write backslash \ in Java's .properties file?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template