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

熱點內容
excel表格文件化亂碼 瀏覽:986
win7無法打開隱藏文件 瀏覽:757
怎樣把壓縮文件變成空白文件 瀏覽:516
wokrNC和UG編程哪個好 瀏覽:487
愛奇藝隨刻文件名 瀏覽:40
wps是怎麼壓縮文件 瀏覽:986
dos配置文件名 瀏覽:582
java軟體工程師做什麼 瀏覽:897
3dmax導出ive文件 瀏覽:464
數據重復性好是什麼意思 瀏覽:166
如何查找電腦文件使用歷史記錄 瀏覽:821
linux可以運行exe的文件嗎 瀏覽:935
庫幣是哪個國家的app 瀏覽:201
大智慧internet文件夾有哪些文件 瀏覽:89
編程培訓課程哪個排名好 瀏覽:124
我的cad怎麼沒顯示文件選項卡 瀏覽:952
命令行當前是哪個資料庫 瀏覽:588
為什麼appstore打開空白 瀏覽:574
選貨車軟體什麼app最好 瀏覽:577
psv仙境傳說ace如何降級版本 瀏覽:460

友情鏈接