本文目錄一覽:
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()時設定的路徑
如何在java中獲取當前項目的路徑
很多朋友都想了解java如何獲取當前項目的路徑?下面就一起來了解一下吧~
在jsp和class文件中調用的相對路徑不同。
在jsp里,根目錄是WebRoot
在class文件中,根目錄是WebRoot/WEB-INF/classes 也可以選用System.getProperty(“user.dir”)獲取工程的絕對路徑。
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: esinwebappsTEST est.jsp (5)得到頁面所在服務器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 結果:D: esinwebappsTEST
2.在class類中取得路徑:
(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:TomcatwebappsTEST (2)得到IE地址欄地址:request.getRequestURL() 結果: (3)得到相對地址:request.getRequestURI() 結果:/TEST/test
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獲取指定資源文件路徑的幾種方法
你好,提問者:
指定資源路徑的方法有兩種:
一種是絕對路徑,一種是相對路徑。
獲取當前類的所在工程路徑;
File f = new File(this.getClass().getResource(“/”).getPath());
System.out.println(f);
獲取當前類的絕對路徑;
File f = new File(this.getClass().getResource(“”).getPath());
System.out.println(f);
獲取當前類的所在工程路徑;
File directory = new File(“”);//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
獲取當前工程src目錄下selected.txt文件的路徑:
URL xmlpath = this.getClass().getClassLoader().getResource(“selected.txt”);
System.out.println(xmlpath);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/297639.html