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

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接