解決Java?properties文件里面如何寫"\"的問題
問題
我的是ssh項(xiàng)目,需要做一個文件上傳,然后文件路徑需要讀取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寫成如下

項(xiàng)目直接啟動不起來,報(bào)了error
解決
經(jīng)過研究,properties使用“\”相當(dāng)于是java的轉(zhuǎn)義符
如果想要寫出\的效果,只需修改如下寫法即可

然后項(xiàng)目起來了,然后看數(shù)據(jù)庫插入的path也正常~

到此這篇關(guān)于Java properties文件里面如何寫“\“的文章就介紹到這了,更多相關(guān)java properties文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java并發(fā)編程synchronized底層實(shí)現(xiàn)原理
這篇文章主要介紹了java并發(fā)編程synchronized底層實(shí)現(xiàn)原理2022-02-02
Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無法立即刷新解決方法示例
這篇文章主要為大家介紹了Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無法立即刷新解決方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Spring中的@ConfigurationProperties在方法上的使用詳解
這篇文章主要介紹了Spring中的@ConfigurationProperties在方法上的使用詳解,@ConfigurationProperties應(yīng)該經(jīng)常被使用到,作用在類上的時候,將該類的屬性取值?與配置文件綁定,并生成配置bean對象,放入spring容器中,提供給其他地方使用,需要的朋友可以參考下2024-01-01
IDEA在一個項(xiàng)目空間下管理多個項(xiàng)目的操作方法
這篇文章主要介紹了IDEA如何在一個項(xiàng)目空間下管理多個項(xiàng)目,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的圖文教程
郵件發(fā)送其實(shí)是一個非常常見的需求,用戶注冊,找回密碼等地方,都會用到,下面這篇文章主要給大家介紹了關(guān)于使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

