iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决Java properties文件里面如何写"\"的问题
  • 332
分享到

解决Java properties文件里面如何写"\"的问题

2024-04-02 19:04:59 332人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

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

问题

我的是ssh项目,需要做一个文件上传,然后文件路径需要读取properties配置
在resource下有config/application.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写成如下

在这里插入图片描述

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

解决

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

在这里插入图片描述

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

在这里插入图片描述

到此这篇关于Java properties文件里面如何写“\“的文章就介绍到这了,更多相关java properties文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 解决Java properties文件里面如何写"\"的问题

本文链接: https://www.lsjlt.com/news/146035.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作