JAVA獲取檔案的幾種常用方式

2021-07-15 02:55:56 字數 1680 閱讀 9123

system.out

.println(system.getproperty("user.dir"));

此方獲取的是執行的路

比如1、

2、如果在eclipse上執行則是eclipse執行檔案同級

3、tomcat則在

4、file file =newfile("log4j.properties");

這裡的log4j.properties也是在以上型別的目錄下查詢

5、fileinputstreaminput =newfileinputstream("log4j.properties");

與file一樣,在執行路徑上找檔案

方法 string in = logutil.class.getresource("/").getpath();

或者:this

.getclass().getclassloader().getresource("/").getpath()

結果:logutil.class.getresource("/").getpath(); 加個」/」就會列印classes下的絕對路徑

logutil.class.getresource("").getpath(); 就會顯示classes + 包名

tomcat列印如下:

web工程

this.getservletcontext().getrealpath("/")

返回:工程的絕對路徑/返回的也稱為「根」

比如工程:

book

|_web-inf

|_xx.properties

返回d:\book\

inputstream input = logutil.class.getresourceasstream("/log4j.properties");

讀取的是src/log4j.properties 下的檔案

inputstream input = logutil.class.getresourceasstream("log4j.properties");

讀取的是 與logutil同級的log4j.properties

第二種:

getservletconfig().getservletcontext()  

.getresourceasstream("report1.jasper")

訪問的是根路徑

book

|_web-inf

|_xx.properties

|_report1.jasper

Java獲取路徑的幾種方式

private static string file name zxl.txt 第一種 new classpathresource 檔名 classpathresource classpathresource new classpathresource file name file file cla...

獲取檔案大小的幾種方式

1 通過檔案c標準課fseek fell獲得,間接獲得,速度慢 include include int main int argc,char argv fseek fp,0,seek end size ftell fp fclose fp printf the size of file is db ...

獲取檔案大小的幾種方式

1 通過檔案c標準io庫函式fseek fell獲得,間接獲得,速度慢 include include int main int argc,char ar fseek fp,0,seek end size ftell fp fclose fp printf the size of file is d...