導航:首頁 > 版本升級 > 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獲取文件路徑相關的資料

熱點內容
cdr導出圖片不顯示文件名 瀏覽:761
pcdmis如何離線編程 瀏覽:201
微信推文插入文件 瀏覽:844
生產文件櫃圖片 瀏覽:244
如何設置微信數據增加 瀏覽:95
excel文件不再增大 瀏覽:613
ug編程刀路怎麼復制不用滑鼠選取 瀏覽:97
excel文件打開擴展名錯誤 瀏覽:10
阿里巴巴怎麼做數據包 瀏覽:442
無線網路連接一直顯示未連接 瀏覽:25
windows7如何自動激活工具下載 瀏覽:57
天語救磚工具 瀏覽:839
網路體育課程有哪些 瀏覽:19
蘋果微信怎麼查看文件 瀏覽:165
安卓nba2k14面補替換 瀏覽:450
路由器密碼定時換 瀏覽:626
aicc恢復文件在哪裡 瀏覽:167
怎麼查無線網路屬性 瀏覽:874
只狼的聲音文件放在哪裡 瀏覽:219
lol盒子修復在哪個文件夾 瀏覽:308

友情鏈接