Java获取当前项目路径(在javaEE的项目中,怎样获取一个文件的全路径)

2024-02-04 14:50:02 :34

java获取当前项目路径(在javaEE的项目中,怎样获取一个文件的全路径)

本篇文章给大家谈谈java获取当前项目路径,以及在javaEE的项目中,怎样获取一个文件的全路径对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

本文目录

在javaEE的项目中,怎样获取一个文件的全路径

  File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:123456789101112131415 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\baiduG:\xhuoj\konw\src\baidu

如何获得当前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项目的目录

一 相对路径的获得说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)String relativelyPath=System.getProperty("user.dir"); 上述相对路径中,java项目中的文件是相对于项目的根目录web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin)二 类加载目录的获得(即当运行时某一类时获得其装载目录)1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt"); (test.txt文件的路径为 项目名\src\test.txt;类TestAction所在包的第一级目录位于src目录下)上式中将TestAction,test.txt替换成对应成相应的类名和文件名字即可***隐藏网址***InputStream is=Test1.class.getResourceAsStream("/test.txt"); (test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下)三 web项目根目录的获得(发布之后)1 从servlet出发可建立一个servlet在其的init方法中写入如下语句ServletContext s1=this.getServletContext();String temp=s1.getRealPath("/"); (关键) 结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext为项目名字)如果是调用了s1.getRealPath("")则输出D:\工具\Tomcat-6.0\webapps\002_ext(少了一个"\")***隐藏网址***String cp11111=request.getSession().getServletContext().getRealPath("/");结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\四 classpath的获取(在Eclipse中为获得src或者classes目录的路径)方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();System.out.println("t---"+t);输出:t---/E:/order/002_ext/WebRoot/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--/E:/order/002_ext/WebRoot/WEB-INF/classes/另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)eg String p2=JdomParse.class.getResource("").getPath(); System.out.println("JdomParse.class.getResource---"+p2);输出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse为src目录下jdom包中的类)四 属性文件的读取:方法 一InputStream in = lnew BufferedInputStream( new FileInputStream(name));    Properties p = new Properties();   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

java里面怎么样获取工程路径

getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)例如 项目在/D:/workspace/MainStream/Test在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/123456789101112 public String getCurrentPath(){ //取得根目录路径 String rootPath=getClass().getResource("/").getFile().toString(); //当前目录路径 String currentPath1=getClass().getResource(".").getFile().toString(); String currentPath3=getClass().getResource("").getFile().toString(); //当前目录的上级目录路径 String parentPath=getClass().getResource("../").getFile().toString(); return rootPath; }

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 获取项目本地路径

因为你的项目的执行文件文件就在tomcat路径下呀,你可以把tomcat的项目路径指定为你项目的路径,这样运行文件就是在你的项目下了,获取的路径就是你真正项目的路径了

怎样在JAVA文件中获取该项目的相对路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。

OK,关于java获取当前项目路径和在javaEE的项目中,怎样获取一个文件的全路径的内容到此结束了,希望对大家有所帮助。

java获取当前项目路径(在javaEE的项目中,怎样获取一个文件的全路径)

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.