導航:首頁 > 編程語言 > java取當前項目路徑

java取當前項目路徑

發布時間:2024-07-01 09:51:01

⑴ 如何獲得當前java文件的路徑

1、利用System.getProperty()函數獲取當前路徑:

System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:

File directory = new File("");//設定為當前文件夾

try{

System.out.println(directory.getCanonicalPath());//獲取標準的路徑

System.out.println(directory.getAbsolutePath());//獲取絕對路徑

}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new
File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾

# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑

# 至於getPath()函數,得到的只是你在new File()時設定的路徑


Java基礎知識教程:

⑵ JAVA怎樣獲取當前路徑

java獲取當前路徑的幾種方法
1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾
try{
System.out.println(directory.getCanonicalPath());//獲取標準的路徑
System.out.println(directory.getAbsolutePath());//獲取絕對路徑
}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾
# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑
# 至於getPath()函數,得到的只是你在new File()時設定的路徑

比如當前的路徑為 C:/test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:/test/abc
directory.getAbsolutePath(); //得到的是C:/test/abc
direcotry.getPath(); //得到的是abc

File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:/test
directory.getAbsolutePath(); //得到的是C:/test/.
direcotry.getPath(); //得到的是.

File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:/
directory.getAbsolutePath(); //得到的是C:/test/..
direcotry.getPath(); //得到的是..

⑶ java中類載入路徑和項目根路徑獲取的幾種方式

// 第一種:獲取類載入的根路徑 D:\git\tie\tie\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
// 獲取當前類的所在工程路徑; 如果不加「/」 獲取當前類的載入目錄 D:\git\tie\tie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二種:獲取項目路徑 D:\git\tie\tie
File directory = new File("");// 參數為空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三種: file:/D:/git/tie/tie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);
// 第四種: D:\git\tie\tie
System.out.println(System.getProperty("user.dir"));/** 結果: C:\Documents and Settings\Administrator\workspace\projectName
* 獲取當前工程路徑*/// 第五種: 獲取所有的類路徑 包括jar包的路徑

⑷ 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");

⑸ java中怎樣獲取當前路徑的絕對路徑

在jsp和class文件中調用的相對路徑不同。在jsp里,根目錄是WebRoot 在class文件中,根目錄是WebRoot/WEB-INF/classes 當然你也可以用System.getProperty("user.dir")獲取工程的絕對路徑。
另:在Jsp,Servlet,Java中詳細獲得路徑的方法!
1.jsp中取得路徑:

以工程名為TEST為例:

(1)得到包含工程名的當前頁面全路徑:request.getRequestURI()
結果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
結果:/TEST
(3)得到當前頁面所在目錄下全名稱:request.getServletPath()
結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp
(4)得到頁面所在伺服器的全路徑:application.getRealPath("頁面.jsp")
結果:D:\resin\webapps\TEST\test.jsp
(5)得到頁面所在伺服器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
結果:D:\resin\webapps\TEST

2.在類中取得路徑:

(1)類的絕對路徑:Class.class.getClass().getResource("/").getPath()
結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路徑:System.getProperty("user.dir")
結果:D:\TEST

3.在Servlet中取得路徑:

(1)得到工程目錄:request.getSession().getServletContext().getRealPath("") 參數可具體到包名。
結果:E:\Tomcat\webapps\TEST
(2)得到IE地址欄地址:request.getRequestURL()
結果:http://localhost:8080/TEST/test
(3)得到相對地址:request.getRequestURI()
結果:/TEST/test

⑹ 用Java獲得項目的路徑

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

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

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

public String getCurrentPath(){
//取得根目錄路徑
String rootPath=getClass().getResource("/").getFile().toString();
//當前目錄路徑
String currentPath1=getClass().getResource(".").getFile().toString();
String currentPath2=getClass().getResource("").getFile().toString();
//當前目錄的上級目錄路徑
String parentPath=getClass().getResource("../").getFile().toString();

return rootPath;

}

⑺ java獲取指定資源文件路徑的幾種方法

你好,提問者:

指定資源路徑的方法有兩種:

一種是絕對路徑,一種是相對路徑。

獲取當前類的所在工程路徑;
Filef=newFile(this.getClass().getResource("/").getPath());
System.out.println(f);
獲取當前類的絕對路徑;
Filef=newFile(this.getClass().getResource("").getPath());
System.out.println(f);
獲取當前類的所在工程路徑;
Filedirectory=newFile("");//參數為空
StringcourseFile=directory.getCanonicalPath();
System.out.println(courseFile);
獲取當前工程src目錄下selected.txt文件的路徑:
URLxmlpath=this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
閱讀全文

與java取當前項目路徑相關的資料

熱點內容
查軟體命令 瀏覽:586
u盤文件夾加了個叉 瀏覽:82
新手程序員項目一直發不上去 瀏覽:784
連續arq協議演算法 瀏覽:593
APP工作目標和實施計劃怎麼寫 瀏覽:559
浙江蘋果開發源碼交付 瀏覽:179
現代漢語語法pdf 瀏覽:80
pdf製作技巧 瀏覽:610
免費解壓啤酒視頻 瀏覽:40
貴州雲存儲伺服器 瀏覽:557
高中生做程序員的規劃 瀏覽:813
領克app怎麼綁定車輛別人的車 瀏覽:639
外語教學pdf 瀏覽:40
程序員釋義 瀏覽:251
數控g71編程時應注意什麼 瀏覽:413
捷聯慣導演算法心得 瀏覽:146
c4d命令的理解 瀏覽:568
pdf文檔水印 瀏覽:917
高斯模糊演算法java 瀏覽:354
小學樂高機器人編程作品 瀏覽:522