本文目錄一覽:
- 1、在java項目中如何獲取某個文件的路徑
- 2、java 項目如何獲取項目所在的物理根路徑
- 3、java如何獲取類的絕對路徑
- 4、JAVA中如何得到文件路徑
- 5、java 獲取項目本地路徑?
- 6、通過java獲取當前項目路徑
在java項目中如何獲取某個文件的路徑
File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(“.\\src\\baidu”);
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\baidu
G:\xhuoj\konw\src\baidu
java 項目如何獲取項目所在的物理根路徑
在java中獲得文件的路徑在我們做上傳文件操作時是不可避免的。web上運行1:this.getClass().getClassLoader().getResource(“/”).getPath();this.getClass().getClassLoader().getResource(“”).getPath();得到的是ClassPath的絕對URI路徑。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/System.getProperty(“user.dir”);this.getClass().getClassLoader().getResource(“.”).getPath();得到的是項目的絕對路徑。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war2:this.getClass().getResource(“/”).getPath();this.getClass().getResource(“”).getPath();得到的是當前類文件的URI目錄。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/this.getClass().getResource(“.”).getPath();X不能運行3:Thread.currentThread().getContextClassLoader().getResource(“/”).getPath()Thread.currentThread().getContextClassLoader().getResource(“”).getPath()得到的是ClassPath的絕對URI路徑。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/Thread.currentThread().getContextClassLoader().getResource(“.”).getPath()得到的是項目的絕對路徑。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war在本地運行中1:this.getClass().getClassLoader().getResource(“”).getPath();this.getClass().getClassLoader().getResource(“.”).getPath();得到的是ClassPath的絕對URI路徑。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesthis.getClass().getClassLoader().getResource(“.”).getPath();X不能運行2:this.getClass().getResource(“”).getPath();this.getClass().getResource(“.”).getPath();得到的是當前類文件的URI目錄。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper//D:/myProjects/hp/WebRoot/WEB-INF/classes/得到的是ClassPath的絕對URI路徑。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes3:Thread.currentThread().getContextClassLoader().getResource(“.”).getPath()Thread.currentThread().getContextClassLoader().getResource(“”).getPath()得到的是ClassPath的絕對URI路徑。。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource(“/”).getPath()X不能運行最後在Web應用程序中,我們一般通過ServletContext.getRealPath(”/”)方法得到Web應用程序的根目錄的絕對路徑。還有request.getContextPath();在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic伺服器,項目內部並沒有文件結構的概念,用這種方式是始終得到null,獲取不到路徑,目前還沒有找到具體的解決方案。
java如何獲取類的絕對路徑
1 用servlet獲取
1.1 獲取項目的絕對路徑
request.getSession().getServletContext().getRealPath(“”)
1.2 獲取瀏覽器地址
request.getRequestURL()
1.3 獲取當前文件的絕對路徑
request.getSession().getServletContext().getRealPath(request.getRequestURI())
2.獲取當前的classpath路徑
String a2=類名.class.getResource(“”).toString();
String a3=DBConnection.class.getResource(“/”).toString();
String a4=DBConnection.class.getClassLoader().getResource(“”).toString();
String t=Thread.currentThread().getContextClassLoader().getResource(“”).getPath();
//輸出很好理解
3、獲取文件的絕對路徑
String t=Thread.currentThread().getContextClassLoader().getResource(“”).getPath();
int num=t.indexOf(“.metadata”);
String path=t.substring(1,num).replace(‘/’, ‘\\’)+”項目名\\WebContent\\文件”;
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
java 獲取項目本地路徑?
因為你的項目的執行文件文件就在tomcat路徑下呀,你可以把tomcat的項目路徑指定為你項目的路徑,這樣運行文件就是在你的項目下了,獲取的路徑就是你真正項目的路徑了
通過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;
}
參考資料:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/188442.html