您的位置:

java获取路径,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()时设定的路径

如何在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);