解決Java?properties文件里面如何寫"\"的問題
問題
我的是ssh項(xiàng)目,需要做一個(gè)文件上傳,然后文件路徑需要讀取properties配置
在resource下有config/application.properties
然后工具類是這樣寫的,這個(gè)是可以用的
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)目直接啟動(dò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)文章
springMVC的RequestMapping請求不到路徑的解決
這篇文章主要介紹了springMVC的RequestMapping請求不到路徑的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解
這篇文章主要介紹了java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10SpringBoot 使用 FTP 操作文件的過程(刪除、上傳、下載文件)
這篇文章主要介紹了SpringBoot 使用 FTP 操作文件,主要包括配置ftp服務(wù)器,上傳、刪除、下載文件操作,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Spring實(shí)戰(zhàn)之獲得Bean本身的id操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲得Bean本身的id操作,結(jié)合實(shí)例形式分析了spring獲取Bean本身id的相關(guān)配置與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11Springboot接口返回參數(shù)及入?yún)SA加密解密的過程詳解
這篇文章主要介紹了Springboot接口返回參數(shù)及入?yún)SA加密解密,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07