導航:首頁 > 文件目錄 > java獲取項目文件路徑

java獲取項目文件路徑

發布時間:2023-06-16 15:46:41

1. 通過java獲取當前項目路徑

getClass().getResource() 方法獲得相對路徑( 此方法在jar包中無效。返回的內容最後包含/)

例如 項目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/

publicStringgetCurrentPath(){
//取得根目錄路徑
StringrootPath=getClass().getResource("/").getFile().toString();
//當前目錄路徑
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//當前目錄的上級目錄路徑
StringparentPath=getClass().getResource("../").getFile().toString();

returnrootPath;

}

2. 在java項目中如何獲取某個文件的路徑

如果是在tomcat等伺服器中運行的話,用ServletContext中的getRealPath()方法可以獲取指定文件的絕對路徑,如:getRealPath("/WEB-INF/db.xml");

3. 在javaEE的項目中,怎樣獲取一個文件的全路徑

File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}

getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\
G:\xhuoj\konw\src\

4. 如何獲得當前Java文件的路徑

public class Test {
public static void main(String[] args) {
String path = "Test.java";
File file = new File(path);
System.out.println(file.getAbsoluteFile());
}
}

-----
運行結果:
D:\workspaces\studyStruts2\Test.java
不加任何路徑,就是指當版前路徑
望採納權

5. java 獲取項目本地路徑

因為你的項目的執行文件文件就在tomcat路徑下呀,你可以把tomcat的項目路徑指定為你項目的路徑,這樣運行文件就是在你的項目下了,獲取的路徑就是你真正項目的路徑了

6. java項目中文件的路徑

java項目中文件的路徑-方法大全

一、 相對路徑的獲得

說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的java項目還是web項目)

System.getProperty("user.dir");

上述相對路徑中,java項目中的文件是相對於項目的根目錄web項目中的文件路徑視不同的web伺服器不同而不同(tomcat是相對於tomcat安裝目錄in)

二 類載入目錄的獲得(即當運行時某一類時獲得其裝載目錄)
1.1)通用的方法一(不論是一般的java項目還是web項目,先定位到能看到包路徑的第一級目錄)

InputStreamis=TestAction.class.getClassLoader().getResourceAsStream("test.txt");(test.txt文件的路徑為 項目名src est.txt;類TestPath所在包的第一級目錄位於src目錄下)

三 web項目根目錄的獲得(發布之後)
1 從servlet出發

可建立一個servlet在其的init方法中寫入如下語句(沒有請求的話會拋空指針導常)

ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (關鍵)
結果形如:F: omcat-6.0.36webapps est(test為項目名字)

如果是調用了s1.getRealPath("")則輸出F: omcat-6.0.36webapps est(少了一個"")

2 從httpServletRequest出發(沒有請求的話會拋空指針導常)

String path=request.getSession().getServletContext().getRealPath("/");

結果形如:F: omcat-6.0.36webapps est

四 classpath的獲取(在Eclipse中為獲得src或者classes目錄的路徑),放在監聽器,可以窗口啟動獲取路徑

方法一Thread.currentThread().getContextClassLoader().getResource("").getPath()

String path = Thread.currentThread().getContextClassLoader()

.getResource("").getPath();

System.out.println("path========"+ path);輸出:path========/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/


方法二JdomParse.class.getClassLoader().getResource("").getPath()(JdomParse為src某一個包中的類,下同)

eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);

輸出:JdomParse.class.getClassLoader().getResource-/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/

另外,如果想把文件放在某一包中,則可以 通過以下方式獲得到文件(先定位到該包的最後一級目錄)

eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);

輸出:JdomParse.class.getResource--/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/

(JdomParse為src目錄下jdom包中的類)

四 屬性文件的讀取:

方法 一

InputStream in = lnewBufferedInputStream(newFileInputStream(name));

Properties p =newProperties();p.load(in);

注意路徑的問題,做執行之後就可以調用p.getProperty("name")得到對應屬性的值

方法二

Locale locale =Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest",locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);

工程src目錄下propertiesTest.properties(名字後綴必須為properties)文件內容如下:

test=hello word

不通過Servlet獲取路徑

第一種實現

Java代碼

URL url = ClassLoader.getSystemClassLoader().getResource("./");

File file =newFile(url.getPath());

File parentFile =newFile(file.getParent());

System.out.println("webRoot:"+parentFile.getParent());

第二種實現
首先寫一個接聽類 (推薦使用,容器啟動時就執行,不會拋空指針異常,適合做定時器任務來刪除伺服器文件的路徑)

Java代碼:

package com.chinacreator.report.listener;

import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

/**

* @authorxiaoqun.yi

*/

public class PathListener {

private staticServletContext servletContext;

public voidcontextDestroyed(ServletContextEvent sce) {

this.servletContext= sce.getServletContext();

System.out.println("path=======:"+servletContext.getRealPath("/"));

}

public voidcontextInitialized(ServletContextEvent arg0) {

}

}


在web.xml中加入如下配置

Java代碼 :

<listener>

<listener-class>com.chinacreator.report.listener.PathListener</listener-class>

</listener>

五、Java中的getResourceAsStream有以下幾種:
1. Class.getResourceAsStream(String path) : path 不以』/'開頭時默認是從此類所在的包下取資源,以』/'開頭則是從ClassPath根下獲取。其只是通過path構造一個絕對路徑,最終還是由 ClassLoader(類載入器)(獲取資源)

2. Class.getClassLoader.getResourceAsStream(String path) :默認則是從ClassPath根下獲取,path不能以』/'開頭,最終是由ClassLoader獲取資源。

3. ServletContext. getResourceAsStream(String path):默認從WebAPP根目錄下取資源,Tomcat下path是否以』/'開頭無所謂,當然這和具體的容器實現有關。

4. Jsp下的application內置對象就是上面的ServletContext的一種實現。

其次,getResourceAsStream 用法大致有以下幾種:

第一: 要載入的文件和.class文件在同一目錄下,例如:com.x.y 下有類me.class ,同時有資源文件myfile.xml

那麼,應該有如下代碼:

me.class.getResourceAsStream("myfile.xml");

第二:在me.class目錄的子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.y.file 目錄下有資源文件myfile.xml

那麼,應該有如下代碼:

me.class.getResourceAsStream("file/myfile.xml");

第三:不在me.class目錄下,也不在子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.file 目錄下有資源文件myfile.xml

那麼,應該有如下代碼:

me.class.getResourceAsStream("/com/x/file/myfile.xml");

總結一下,可能只是兩種寫法

第一:前面有 「 / 」

「 / 」代表了工程的根目錄,例如工程名叫做myproject,「 / 」代表了myproject

me.class.getResourceAsStream("/com/x/file/myfile.xml");

第二:前面沒有 「 / 」

代表當前類的目錄

me.class.getResourceAsStream("myfile.xml");

me.class.getResourceAsStream("file/myfile.xml");

7. JAVA中如何得到文件路徑

java文件中獲得路徑
Thread.currentThread().getContextClassLoader().getResource("") //獲得資源文件(.class文件)所在路徑
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 獲得當前類所在路徑
System.getProperty("user.dir") // 獲得項目根目錄的絕對路徑
System.getProperty("java.class.path") //得到類路徑和包路徑
列印輸出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

8. java怎麼獲取文件的相對路徑

一個例子,如果有一個Test文件夾裡面有test.java和hello.txt.如果你想用test.java操作hello.txt
只要在test.java中這樣寫File file=new File("hello.txt");//這樣就是相對路徑。如果文件結構是
Test文件夾
... |------test.java
... |------hello.txt
... |------source文件夾
................. |---------world.txt
如果想在test.java中操作world.txt。只要這樣寫File file=new File("source/world.txt");
另外,在web開發中/代表項目文件夾根目錄,當然也有可能代替webapps,區分方法是:如果/開頭的uri是給瀏覽器解析則/代表webapps,如果是給伺服器後台解析,則代表項目文件

9. java中獲取文件路徑的幾種方式

獲取當前類的所在工程路徑;
如果不加「/」
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);結果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;第二種:File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;第三種:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);結果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑第四種:System.out.println(System.getProperty("user.dir"));結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑第五種:System.out.println( System.getProperty("java.class.path"));結果:C:\Documents and Settings\Administrator\workspace\projectName\bin獲取當前工程路徑

閱讀全文

與java獲取項目文件路徑相關的資料

熱點內容
美服applestoreid 瀏覽:889
微信數據文件能清嗎 瀏覽:107
圖片編輯到文件里列印怎麼有重影 瀏覽:593
舟山四軸編程培訓哪個學校好 瀏覽:542
沒加別人QQ可以發word文件 瀏覽:51
手機桌面的文件路徑 瀏覽:892
改款新寶來怎麼使用手機app 瀏覽:281
dede工具 瀏覽:507
5g網盟app怎麼下載 瀏覽:486
微信備份老是連接中斷 瀏覽:886
出台多少份文件 瀏覽:380
鞋子怎麼搭配衣服的app 瀏覽:755
文件名使用的通配符的符號是什麼 瀏覽:916
lol分卷文件損壞怎麼辦 瀏覽:276
6分管車螺紋怎麼編程 瀏覽:732
海口農商銀行信用卡app是什麼 瀏覽:770
win10任務欄文件夾我的電腦 瀏覽:14
安卓nba2k18 瀏覽:776
文件夾密碼怎麼修改密碼 瀏覽:271
蘋果數據中心用什麼伺服器 瀏覽:769

友情鏈接