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

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

PHPz
PHPzforward
2023-04-23 16:01:071639browse

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"));
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete