搜索
首页Javajava教程如何在Java的.properties文件中写入反斜线\?

问题

我的是ssh项目,需要做一个文件上传,然后文件路径需要读取properties配置
在resource下有config/application.properties

Java properties文件里怎么写"\"

然后工具类是这样写的,这个是可以用的

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

如果我在properties写成如下

Java properties文件里怎么写"\"

项目直接启动不起来,报了error

解决

经过研究,properties使用“\”相当于是java的转义符
如果想要写出\的效果,只需修改如下写法即可

Java properties文件里怎么写"\"

然后项目起来了,然后看数据库插入的path也正常~

Java properties文件里怎么写"\"

以上是如何在Java的.properties文件中写入反斜线\?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境