導航:首頁 > 版本升級 > bootstrap獲取文件路徑

bootstrap獲取文件路徑

發布時間:2023-03-15 06:12:21

java web工程中讀取properties文件,路徑一直不知道怎麼寫

1. 使用java.lang.Class類的getResourceAsStream(String name)方法

InputStreamin=getClass().getResourceAsStream("/config.properties");

在靜態方法中,由於不能使用getClass()方法,必須寫出類的名字。區別不大。

MyClass.class.getResourceAsStream("/config.properties");

使用這個方法,路徑前面可以加斜杠也可以不加。根據Class類getResourceAsStream()方法的JavaDoc:

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:

modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').

就是說,這個path假如以斜杠開頭,則斜杠後面的部分是文件相對classpath的路徑;假如不是,Java會把這個path看作是「包名/文件名」的結構,會嘗試在這個類的包裡面去找,而不是從classpath開始找;在這種情況下,除非你把properties文件放到MyClass.class所屬的包裡面,不然都會是null的。

2. 使用java.lang.ClassLoader類的getResourceAsStream(String name)方法


路徑是不能加斜杠的!非常重要。

MyClass.class.getClassLoader().getResourceAsStream("config.properties");

這是因為使用classloader進行讀取,所輸入的參數必須是一個相對classpath的絕對路徑,在格式上,一個絕對路徑是不能以'/'開頭的。

注意這兩個方法是同名的,但路徑參數的格式截然不同。


3. 在Maven中的運用

現在幾乎所有的web project都是maven project,Maven的默認設置是把

src/main/resources/

加入到classpath裡面的。那麼,最好的做法是把你的properties文件放進src/main/resources裡面,然後用上面代碼讀取。用Class類的,一般要加斜杠;用ClassLoader類的,絕不能加斜杠!

假如是Eclipse裡面,需要把這個src/main/resources加到classpath裡面。具體操作是右擊工程,選擇「Configure buildpath」,根據Maven的要求,把src/main/java和src/main/resources都加進去,並且保證Exclude是none,Include是all,或者至少要包括你需要讀取的文件。

Ⅱ 為什麼我的bootstrap用不了,我確定路徑沒錯,jq寫在bootstrap的前面,算上只用載入三個文件就好了吧

bootstrap前端框架本地引用:

<!--CSS樣式-->
<linkrel="stylesheet"href="common/styles/libs/bootstrap.css"><!--Bootstrap-->
<linkrel="stylesheet"href="common/styles/libs/font-awesome.css"><!--Fontawesomeicon-->

<!--js(注意jquery.js須在bootstrap.js的前面引用)-->
<scripttype="text/javascript"src="common/scripts/libs/jquery.js"></script>
<scripttype="text/javascript"src="common/scripts/libs/bootstrap.js"></script>


除了上述本地引用的方法外,還可以引用在線CDN:
<linkrel="stylesheet"href="cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css"><!--Bootstrap(min壓縮版)-->
<scripttype="text/javascript"src="cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
<scripttype="text/javascript"src="cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>



說明:
一般css樣式在<head></head>標簽中引用;
JS文件在<body></body>標簽結束前引用,並注意引用順序。

Ⅲ centos7 安裝boots,提示bash: ./bootstrap.sh: 沒有那個文件或目錄

要到腳本所在目錄才能這樣執行
不然就寫腳本的絕對路徑

Ⅳ 怎麼重新配置bootstrap.jar的路徑

可以雙擊該server配置。
1.錯誤症狀:右擊tomcat server,選擇start,出現下圖所示錯誤2.錯誤原因:我為了方便管理,把tomcat安裝到了當前的eclipse-project目錄下:E:/workspace/xxx_project/apache-tomcat-6.0.37,如下圖所示另外,也有可能是bootstrap.jar的路徑配置錯誤,可以雙擊該server配置,見解決方案3.解決方案 針對我目前的問題,把tomcat的安裝路徑挪到另一個位置即可,不要安裝到跟你的web-project的目錄在同一個位置(尤其是eclipse創建的project),例如E:/apache-tomcat-6.0.37
在計算機中,路徑是指用戶在磁碟上搜索文件時所經過的文件夾路徑。路徑分為絕對路徑和相對路徑。

閱讀全文

與bootstrap獲取文件路徑相關的資料

熱點內容
a790e線刷教程 瀏覽:962
win10系統如何復制Pdf文件 瀏覽:800
js滑鼠移動圖顯示數據 瀏覽:203
微信網名帶念字 瀏覽:161
提取一個文件夾里的所有圖片 瀏覽:198
三星s5自帶文件管理器 瀏覽:356
如何將文檔數據按規定排序 瀏覽:660
word中的文件如何重命名 瀏覽:272
新基建中的數據中心是指什麼 瀏覽:728
pdf掃描文件插入圖片大小 瀏覽:362
java500錯誤原因 瀏覽:905
sx1278代碼 瀏覽:729
調試配置文件 瀏覽:780
企業培訓app如何選 瀏覽:38
adonet讀取資料庫 瀏覽:717
藍色代碼 瀏覽:500
怎麼進入網站後台管理系統 瀏覽:988
win7如何傳文件 瀏覽:649
微信贈送u盤文件 瀏覽:216
office2016激活文件名 瀏覽:841

友情鏈接